ASCII Plotting Functions for R [duplicate] - r

This question already has answers here:
How can I generate ascii "graphical output" from R?
(2 answers)
Closed 10 years ago.
In order to access my server, I am forced to work with an old text terminal application that does not have X windows. The best thing I have going is emacs/ESS.
Often, I wish to make a rudimentary plots such as histograms and scatter plots and not have to go through the trouble of transferring the file to a computer with a graphics display.
Is there a text terminal-based R graphics library?

There are a number of things that can do bits of it. There's stem in default R, there's this scatter plot function, but best of all, there's the package txtplot on CRAN which does scatterplots, boxplots, barplots, density traces, acfs and plots curves (like the curve function... kinda).
I only need it once in a while - but if I am trying to convey a rough idea of a graphic in pure text as I sometimes need to, it's a life-saver.
In the past I wrote a short piece of R code that made tally-style ascii graphic in very quick time (like a sideways barchart or a stem-and-leaf plot with the numbers replaced by symbols, which solved a problem I had) - but I didn't keep it since stem mostly covers that territory.
Of course the 'table' facility produces ascii output and can be manipulated to do some interesting/useful semigraphical things.
There's also the package ascii that can be used to render various R objects into ascii form in similar fashion to Sweave - handy for formatting tables and so on. Just formatting a table into ascii is not really what it's for but you might still be able to get some use out of it with a little work and the right output format.
Sample output from txtplot:
scatter plot:
> with(cars,txtplot(speed,dist))
+----+-----------+------------+-----------+-----------+--+
120 + * +
| |
100 + +
| * * |
80 + * * +
| * * * |
60 + * * +
| * * * * * |
40 + * * * * * +
| * * * * * * * |
20 + * * * * * * * +
| * * * * |
| * * * |
0 +----+-----------+------------+-----------+-----------+--+
5 10 15 20 25
acf plot:
> txtacf(ldeaths)
+-+--------------+--------------+--------------+--------+
1 + * +
| * |
| * * * * * |
0.5 + * * * * * +
| * * * * * * * * |
| * * * * * * * * |
| * * * * * * * * |
0 + * * * * * * * * * * * * * * * * * * * * * +
| * * * * * * * * * * |
| * * * * * * * * * * |
| * * * * * * * * * |
-0.5 + * * * * * * +
| * * * * |
+-+--------------+--------------+--------------+--------+
0 0.5 1 1.5
density trace:
> txtdensity(rnorm(100,m=5,s=.1))
+------+----------+----------+----------+----------+-------+
| ***** |
4 + ** *** +
| * *** |
| ** *** |
3 + ** *** +
| *** ** |
| ***** ** |
2 + *** ** +
| *** ** |
| ** ** |
1 + ** *** +
| *** ****** |
| ******** *** |
+------+----------+----------+----------+----------+-------+
4.8 4.9 5 5.1 5.2
box plot:
> vc <- ToothGrowth[,2]=="VC"
> oj <- ToothGrowth[,2]=="OJ"
> txtboxplot(ToothGrowth[vc,1],ToothGrowth[oj,1])
5 10 15 20 25 30 35
|----+-------+--------+--------+--------+--------+-------+--|
+--------+-----------+
1 -------------| | |------------------
+--------+-----------+
+------------+----+
2 -------------| | |---------
+------------+----+
Legend: 1=ToothGrowth[vc, 1], 2=ToothGrowth[oj, 1]
curve plot:
> txtcurve(sin(pi*x),from=0,to=2)
+--+-----------+------------+------------+-----------+--+
1 + ********* +
| *** ** |
| ** ** |
0.5 + ** ** +
| ** ** |
| * ** |
0 + * ** * +
| * * |
| ** ** |
-0.5 + *** ** +
| ** ** |
| ** *** |
-1 + ********* +
+--+-----------+------------+------------+-----------+--+
0 0.5 1 1.5 2
bar chart:
> txtbarchart(as.factor(res),pch="|")
+--+------------+------------+------------+------------+--+
50 + | +
| | |
40 + | +
| | |
30 + | | +
| | | |
| | | |
20 + | | | +
| | | | |
10 + | | | +
| | | | |
0 + | | | +
+--+------------+------------+------------+------------+--+
1 1.5 2 2.5 3
Legend: 1=A, 2=B, 3=C
Add in the stem function from default R graphics:
> stem(log(islands,10))
The decimal point is at the |
1 | 1111112222233444
1 | 5555556666667899999
2 | 3344
2 | 59
3 |
3 | 5678
4 | 012
and you have quite a lot of coverage.

Related

how to combine R data frames with non-exact criteria (greater/less than condition)

I have to combine two R data frames which have trade and quote information. Like a join, but based on a timestamp in seconds. I need to match each trade with the most recent quote. There are many more quotes than trades.
I have this table with stock quotes. The Timestamp is in seconds:
+--------+-----------+-------+-------+
| Symbol | Timestamp | bid | ask |
+--------+-----------+-------+-------+
| IBM | 10 | 132 | 133 |
| IBM | 20 | 132.5 | 133.3 |
| IBM | 30 | 132.6 | 132.7 |
+--------+-----------+-------+-------+
And these are trades:
+--------+-----------+----------+-------+
| Symbol | Timestamp | quantity | price |
+--------+-----------+----------+-------+
| IBM | 25 | 100 | 132.5 |
| IBM | 31 | 80 | 132.7 |
+--------+-----------+----------+-------+
I think a native R function or dplyr could do it - I've used both for basic purposes but not sure how to proceed here. Any ideas?
So the trade at 25 seconds should match with the quote at 20 seconds, and the trade #31 matches the quote #30, like this:
+--------+-----------+----------+-------+-------+-------+
| Symbol | Timestamp | quantity | price | bid | ask |
+--------+-----------+----------+-------+-------+-------+
| IBM | 25 | 100 | 132.5 | 132.5 | 133.3 |
| IBM | 31 | 80 | 132.7 | 132.6 | 132.7 |
+--------+-----------+----------+-------+-------+-------+
Consider merging on a calculated field by increments of 10. Specifically, calculate a column for multiples of 10 in both datasets, and merge on that field with Symbol.
Below transform and within are used to assign and de-assign the helper field, mult10. In this use case, both base functions are interchangeable:
final_df <- transform(merge(within(quotes, mult10 = floor(Timestamp / 10) * 10),
within(trades, mult10 = floor(Timestamp / 10) * 10),
by=c("Symbol", "mult10"),
multi10 = NULL)
Now if the 10 multiple does not suffice for your needs, adjust to level you require such as 15, 5, 2, etc.
within(quotes, mult10 <- floor(Timestamp / 15) * 15)
within(quotes, mult10 <- floor(Timestamp / 5) * 5)
within(quotes, mult10 <- floor(Timestamp / 2) * 2)
Even more, you may need to use the reverse, floor or ceiling for both data sets respectively to calculate highest multiple of quote's Timestamp and lowest multiple of trade's Timestamp:
within(quotes, mult10 <- ceiling(Timestamp / 15) * 15)
within(trades, mult10 <- floor(Timestamp / 5) * 5)

symfony2 createQueryBuilder

I am trying to make a really simple sql query from repository class, just select * from Adjudicacion where cursoAcademico_id=$cursoAcademicoActual;:
This is my entity:
/**
* Adjudicacion
*
* #ORM\Table(name="Adjudicacion")
* #ORM\Entity(repositoryClass="Administrador\AdjudicacionBundle\Entity\AdjudicacionRepository")
*/
class Adjudicacion {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="fechaInicio", type="date")
*/
private $fechaInicio;
/**
* #var \DateTime
*
* #ORM\Column(name="fechaFinal", type="date")
*/
private $fechaFinal;
/**
* #ORM\ManyToOne(targetEntity="Administrador\CursoAcademicoBundle\Entity\CursoAcademico")
*/
private $cursoAcademico;
/**
* #ORM\ManyToOne(targetEntity="Administrador\AdjudicacionClaseBundle\Entity\AdjudicacionClase")
*/
private $adjudicacionClase;
/**
* #ORM\ManyToOne(targetEntity="Administrador\AdjudicacionNumeroBundle\Entity\AdjudicacionNumero")
*/
private $adjudicacionNumero;
/**
* #ORM\ManyToOne(targetEntity="Administrador\AdjudicacionTipoBundle\Entity\AdjudicacionTipo")
*/
private $adjudicacionTipo;
...getters and setters...
This is my repository class:
class AdjudicacionRepository extends EntityRepository {
public function findAdjudicacionesActuales($cursoAcademicoActual) {
$q=$this->createQueryBuilder('c')
->where('c.cursoAcademico_id = :cursoAcademico_id')
->setParameter('cursoAcademico_id', $cursoAcademicoActual)
->getQuery()->getResult();
return $q;
}
}
But it doesnt work, the screen is just blank and I dont get any result. I tried with criteria too, like this:
public function findAdjudicacionesActuales2($cursoAcademicoActual) {
$expr = Criteria::expr();
$criteria = Criteria::create();
$criteria->where($expr->eq("cursoAcademico_id", $cursoAcademicoActual));
return $this->matching($criteria);
}
and i get: Unrecognized field: cursoAcademico_id
This is in the database:
mysql> select * from Adjudicacion;
+----+-------------+------------+-------------------+----------------------+-----------------------+---------------------+
| id | fechaInicio | fechaFinal | cursoAcademico_id | adjudicacionClase_id | adjudicacionNumero_id | adjudicacionTipo_id |
+----+-------------+------------+-------------------+----------------------+-----------------------+---------------------+
| 2 | 2009-01-01 | 2009-01-01 | 7 | 3 | 4 | 3 |
| 6 | 2009-01-01 | 2009-01-01 | 7 | 3 | 4 | 4 |
| 7 | 2009-01-01 | 2009-01-01 | 7 | 3 | 5 | 3 |
| 8 | 2009-01-01 | 2009-01-01 | 7 | 3 | 5 | 4 |
+----+-------------+------------+-------------------+----------------------+-----------------------+---------------------+
what is wrong?
Technically in the eyes of Doctrine ORM the cursoAcademico_id field doesn't exist. It is used to create a link between the 2 tables to create the object but you can't use it in anything.
To do a search for an object with the given id you should use a join and match the id of the joined object like..
$q=$this->createQueryBuilder('a')
// Create builder in 'Adjudicacion' repository so 'a' rather than 'c'
->join('a.cursoAcademico', 'c')
// Join 'Adjudicacion' to 'CursoAcademico'
->where('c.id = :cursoAcademico_id')
// match id of joined `CursoAcademico`
->setParameter('cursoAcademico_id', $cursoAcademicoActual)
->getQuery()->getResult();
return $q;

cron job scheduling timing for excecuting the script

I need to execute a script through cron expression that is 5 days per week (Mon-Fri) between 6pm and 7am GMT.
please advise what will be the cron expression for this..
I have tried this as ...
0 00 23 ? * MON-FRI
as the format is as follows:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
you may want it to be:
* 6 * * 1-5 command
This will work every minute from 6.00 to 6.59. If you need it to execute also at 7.00:
* 6 * * 1-5 command
0 7 * * 1-5 command
and if you want from 6.00 to 7.59, every minute:
* 6-7 * * 1-5 command

Symbolic inverse of a matrix in R

How can I find the symbolic inverse of a matrix in R; for example:
Matrix.test <- function(x) matrix(c(x, x^2, x^3, x^4, x^5, x^6, x^7, x^8, x^9, 2*x, 3*x, 4*x, 2*x^2, 3*x^3, 4*x^4, 5*x^5), 4, 4)
I Know there is a package called 'Ryacas' which is an interface to 'yacas', but I could not apply it to do such this computations.
'yacas' is a program for symbolic manipulation of mathematical expressions.
Please see link for more details.
thank you
It works fine for me:
> library(Ryacas)
> x <- Sym('x')
> M <- List(List(1,x),List(x,1))
> PrettyForm(M)
/ \
| ( 1 ) ( x ) |
| |
| ( x ) ( 1 ) |
\ /
> PrettyForm(Inverse(M))
/ \
| / 1 \ / -( x ) \ |
| | ------ | | ------ | |
| | 2 | | 2 | |
| \ 1 - x / \ 1 - x / |
| |
| / -( x ) \ / 1 \ |
| | ------ | | ------ | |
| | 2 | | 2 | |
| \ 1 - x / \ 1 - x / |
\ /
And following on:
M2 <- List(List( x, x^2, x^3, x^4),
List( x^5, x^6, x^7, x^8),
List( x^9, 2*x ,3*x , 4*x),
List(2*x^2, 3*x^3, 4*x^4, 5*x^5))
Inverse(M2)
The answer's pretty complicated, though (I could only be bothered to re-format the first four lines):
{{(x^6*3*x*5*x^5-x^6*4*x*4*x^4+x^8*2*x*4*x^4-x^7*2*x*5*x^5+
x^7*4*x*3*x^3-x^8*3*x*3*x^3)/(x^7*3*x*5*x^5-x^7*4*x*4*x^4-
x^8*2*x*5*x^5+x^8*4*x*3*x^3-x^9*3*x*3*x^3+x^9*3*x*3*x^3+
x^8*2*x*5*x^5-x^8*4*x*3*x^3-x^7*3*x*5*x^5+x^7*4*x*4*x^4-
x^19*4*x^4+x^19*4*x^4+x^18*5*x^5-x^9*4*x*2*x^2-x^18*5*x^5+x^9*4*x*2*x^2-x^11*2*x*2*x^2+x^11*2*x*2*x^2),(x^4*3*x*3*x^3-x^4*2*x*4*x^4+x^3*2*x*5*x^5-x^3*4*x*3*x^3-x^2*3*x*5*x^5+x^2*4*x*4*x^4)/(x^7*3*x*5*x^5-x^7*4*x*4*x^4-x^8*2*x*5*x^5+x^8*4*x*3*x^3-x^9*3*x*3*x^3+x^9*3*x*3*x^3+x^8*2*x*5*x^5-x^8*4*x*3*x^3-x^7*3*x*5*x^5+x^7*4*x*4*x^4-x^19*4*x^4+x^19*4*x^4+x^18*5*x^5-x^9*4*x*2*x^2-x^18*5*x^5+x^9*4*x*2*x^2-x^11*2*x*2*x^2+x^11*2*x*2*x^2),0,(x^10*3*x-x^9*4*x-x^10*3*x+x^9*4*x-x^11*2*x+x^11*2*x)/(x^7*3*x*5*x^5-x^7*4*x*4*x^4-x^8*2*x*5*x^5+x^8*4*x*3*x^3-x^9*3*x*3*x^3+x^9*3*x*3*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4-x^19*4*x^4+x^19*4*x^4+x^18*5*x^5-x^9*4*x*2*x^2-x^18*5*x^5+x^9*4*x*2*x^2-x^11*2*x*2*x^2+x^11*2*x*2*x^2),(x^7*3*x-x^8*2*x+x^8*2*x-x^7*3*x)/(x^7*3*x*5*x^5-x^7*4*x*4*x^4-x^8*2*x*5*x^5+x^8*4*x*3*x^3-x^9*3*x*3*x^3+x^9*3*x*3*x^3+x^8*2*x*5*x^5-x^8*4*x*3*x^3-x^7*3*x*5*x^5+x^7*4*x*4*x^4-x^19*4*x^4+x^19*4*x^4+x^18*5*x^5-x^9*4*x*2*x^2-x^18*5*x^5+x^9*4*x*2*x^2-x^11*2*x*2*x^2+x^11*2*x*2*x^2)}};

Can I calculate the average of these numbers?

I was wondering if it's possible to calculate the average of some numbers if I have this:
int currentCount = 12;
float currentScore = 6.1123 (this is a range of 1 <-> 10).
Now, if I receive another score (let's say 4.5), can I recalculate the average so it would be something like:
int currentCount now equals 13
float currentScore now equals ?????
or is this impossible and I still need to remember the list of scores?
The following formulas allow you to track averages just from stored average and count, as you requested.
currentScore = (currentScore * currentCount + newValue) / (currentCount + 1)
currentCount = currentCount + 1
This relies on the fact that your average is currently your sum divided by the count. So you simply multiply count by average to get the sum, add your new value and divide by (count+1), then increase count.
So, let's say you have the data {7,9,11,1,12} and the only thing you're keeping is the average and count. As each number is added, you get:
+--------+-------+----------------------+----------------------+
| Number | Count | Actual average | Calculated average |
+--------+-------+----------------------+----------------------+
| 7 | 1 | (7)/1 = 7 | (0 * 0 + 7) / 1 = 7 |
| 9 | 2 | (7+9)/2 = 8 | (7 * 1 + 9) / 2 = 8 |
| 11 | 3 | (7+9+11)/3 = 9 | (8 * 2 + 11) / 3 = 9 |
| 1 | 4 | (7+9+11+1)/4 = 7 | (9 * 3 + 1) / 4 = 7 |
| 12 | 5 | (7+9+11+1+12)/5 = 8 | (7 * 4 + 12) / 5 = 8 |
+--------+-------+----------------------+----------------------+
I like to store the sum and the count. It avoids an extra multiply each time.
current_sum += input;
current_count++;
current_average = current_sum/current_count;
It's quite easy really, when you look at the formula for the average: A1 + A2 + ... + AN/N. Now, If you have the old average and the N (numbers count) you can easily calculate the new average:
newScore = (currentScore * currentCount + someNewValue)/(currentCount + 1)
You can store currentCount and sumScore and you calculate sumScore/currentCount.
or... if you want to be silly, you can do it in one line :
current_average = (current_sum = current_sum + newValue) / ++current_count;
:)
float currentScore now equals (currentScore * (currentCount-1) + 4.5)/currentCount ?

Resources