Hi:
After cleaning up your data, here's one way using the plyr and
reshape packages:
d - read.csv(textConnection(
Individual, A, B, C, D
Day1, 1,1,1,1
Day2, 1,3,4,2
Day3, 3,,6,4), header = TRUE)
closeAllConnections()
d
library('plyr')
library('reshape')
# Stack the variables
dm - melt(d, id = 'Individual')
# Convert the new value column to factor as follows:
dm$value - factor(dm$value, levels = c(1:7, NA), exclude = NULL)
# Use ddply() in conjunction with tabulate():
ddply(dm, .(variable), function(d) tabulate(d$value, nbins = 8))
variable V1 V2 V3 V4 V5 V6 V7 V8
1A 2 0 1 0 0 0 0 0
2B 1 0 1 0 0 0 0 1
3C 1 0 0 1 0 1 0 0
4D 1 1 0 1 0 0 0 0
This returns a data frame. If you want a matrix instead, use the
daply() function rather than ddply() and leave everything else the
same.
HTH,
Dennis
On Tue, Nov 1, 2011 at 1:05 PM, Empty Empty phytophthor...@yahoo.com wrote:
Hi,
I am an R novice and I am trying to do something that it seems should be
fairly simple, but I can't quite figure it out and I must not be using the
right words when I search for answers.
I have a dataset with a number of individuals and observations for each day
(7 possible codes plus missing data)
So it looks something like this
Individual A, B, C, D
Day1 1,1,1,1
Day 2 1,3,4,2
Day3 3,,6,4
(I've also tried transposing it so that individuals are rows and days are
columns)
I want to summarize the total observation codes by individual so that I end
up with a table something like this:
, 1, 2 ,3 ,4, 5, 6,7, missing
A 2,0,1,0,0,0,0,0
B 1,0,1,0,0,0,0,1
C 1,0,0,1,0,1,0,0
D 1,1,0,1,0,0,0,0
If I can get that I'll be happy! Part two is
subsetting which days I include in the counts so that I could, say look at
days 1-30 and 30-60 separately - create two different tables from the same
original table. (Or I can do it manually and start with different subsets of
the data).
Thanks so much for any help.
[[alternative HTML version deleted]]
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.