[R] Can somebody help me with following data manipulation?
Christofer Bogaso
bogaso.christofer at gmail.com
Thu Dec 6 20:35:56 CET 2012
Dear all, let say I have following data:
dat <- structure(list(V1 = structure(c(1L, 4L, 5L, 3L, 3L, 5L, 6L, 6L,
4L, 3L, 5L, 6L, 5L, 5L, 4L, 4L, 6L, 2L, 3L, 4L, 3L, 3L, 2L, 5L,
3L, 6L, 3L, 3L, 6L, 3L, 6L, 1L, 6L, 5L, 2L, 2L), .Label = c("C",
"G", "I", "O", "R", "T"), class = "factor"), V2 = c(0L, 0L, 0L,
1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L,
1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L,
0L), V3 = c(1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L,
0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L,
0L, 1L, 0L, 1L, 0L, 1L, 1L)), .Names = c("V1", "V2", "V3"), class =
"data.frame", row.names = c(NA,
-36L))
Now I want to get following kind of data frame out of that:
dat1 <- structure(list(V1 = structure(c(3L, 3L, 1L, 1L, 2L, 2L), .Label
= c("C",
"G", "I"), class = "factor"), V2 = c(0L, 1L, 0L, 1L, 0L, 1L),
V3 = c(0.333333333, 0.428571429, 0.5, NA, 1, NA)), .Names = c("V1",
"V2", "V3"), class = "data.frame", row.names = c(NA, -6L))
Basically in 'dat1', the 3rd column is coming from: for 'V1 = I' & 'V2 =
0' what is the percentage of '1' for "V3" and so on.....
Is there any R function to achieve that directly?
Thanks and regards,
More information about the R-help
mailing list