Skip to content

Counting entries to create a new table

2 messages · Empty Empty, Dennis Murphy

#
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
1        A  2  0  1  0  0  0  0  0
2        B  1  0  1  0  0  0  0  1
3        C  1  0  0  1  0  1  0  0
4        D  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 <phytophthorasb at yahoo.com> wrote: