I have data that I would like to compute some statistics with.
The data is organized in such a way that I have a value corresponding to each 3-element tuple
Something like
(P1,M1,R1,V1)
(P1,M1,R2,V2)
(P1,M2,R1,V1)
...
here P1, M1, and R1 are not numeric but V1 and V2 are.
Right now I have the data in a csv file, x2.cvs as follows:
P,M,R,V
P1,M1,R1,V1
P1,M1,R2,V2
...
I read the data using
d = read.table("x2.csv", sep=",", header=TRUE)
but after that I don’t know what to do to process the data.
I would like to start by computing simple information like:
what is the average for each element of P (so the average would be over all elements
of M and R), or for each pair of elements of {P,M} (so the average here would be over the elements of R.
Next I would like to do a little bit more complicated things like compute how many elements of P1 are bigger than some specified value.
Here’s a start, with
data.table,plyrand base functions, there are so many other ways…First, some example data…
Here is one way using
data.table. If your data object is very big, you’ll find thedata.tablepackage to be very fast, documentation is also excellent: http://datatable.r-forge.r-project.org/datatable-intro.pdfAnd another using
plyrAnd another using base R
And for your last question, how many elements of P1 are bigger than some specified value, we can use
subsetlike so:Or even just
[for the same result with less typing: