[R] Hi , Is it possible select a different number of rows by each group with R????
arun
smartpink111 at yahoo.com
Tue Apr 22 19:45:59 CEST 2014
Hi Marta,
It's not clear whether you wanted to select the first "n" rows specified by field2 in the first dataset or just random rows.
##using a modified example if my guess is correct
dat1 <- structure(list(field1 = 1:3, field2 = c(3L, 6L, 4L)), .Names = c("field1",
"field2"), class = "data.frame", row.names = c(NA, -3L))
dat2 <- structure(list(field1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L), field3 = c(0.375, 0.416667, 0.458333, 0.5, 0.541667, 0.583333,
0.625, 0.666667, 0.708333, 0.75, 0.791667, 0.833333, 0.875, 0.583333,
0.625, 0.666667, 0.708333, 0.75, 0.791667, 0.833333, 0.875, 0.708333,
0.75, 0.791667, 0.833333, 0.875), field4 = c("Sp", "Sp", "Sp",
"Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp",
"Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp", "Sp",
"Sp"), field5 = c("Rm1", "Rm2", "Rm3", "Rm4", "Rm5", "Rm6", "Jm1",
"Jm2", "Jm3", "Jm4", "Jm5", "Jm6", "Jm7", "Rm6", "Jm1", "Jm2",
"Jm3", "Jm4", "Jm5", "Jm6", "Jm7", "Jm3", "Jm4", "Jm5", "Jm6",
"Jm7")), .Names = c("field1", "field3", "field4", "field5"), class = "data.frame", row.names = c(NA,
-26L))
##for selecting the first 'n' rows
dat2New <- merge(dat1,dat2,by="field1")
library(plyr)
res1 <- ddply(dat2New,.(field1),function(x) head(x,unique(x$field2)))[,-2]
#or
res2 <- dat2[with(dat1,rep(match(field1, dat2$field1),field2)+sequence(field2)-1),]
A.K.
Sorry, I think now the message is correct.
Hi , Is it possible select a different number of rows by each group with R????
I must to select different number (specific quantity in field2:Table1) of rows in each group(field1:Table2).
I have these 2 tables:
Table1
field1 field2
1 3
2 6
3 9
4 3
5 3
6 3
7 3
8 9
9 6
10 3
11 3
12 3
13 3
14 3
Table2
field1 field3 field4 field5
1 0.375 Sp Rm1
1 0.416667 Sp Rm2
1 0.458333 Sp Rm3
1 0.5 Sp Rm4
1 0.541667 Sp Rm5
1 0.583333 Sp Rm6
1 0.625 Sp Jm1
1 0.666667 Sp Jm2
1 0.708333 Sp Jm3
1 0.75 Sp Jm4
1 0.791667 Sp Jm5
1 0.833333 Sp Jm6
1 0.875 Sp Jm7
thx!!!
On Monday, April 21, 2014 4:02 PM, Marta Tobeña <marta_mtm at hotmail.com> wrote:
Hi , Is it possible select a different number of rows by each group with R????
I must to select different number (specific quantity in field2:Table1) of rows in each group(field1:Table2). I have these 2 tables:Table1Table2field1field2field1field3field4field51310.375SpRm12610.416667SpRm23910.458333SpRm34310.5SpRm45310.541667SpRm56310.583333SpRm67310.625SpJm18910.666667SpJm29610.708333SpJm310310.75SpJm411310.791667SpJm512310.833333SpJm613310.875SpJm714320.916667SpJm820.958333SpJm921SpJm1021.041667SpJm1121.083333SpJm1221.125SpJm1321.166667SpJm1421.208333SpJm1521.25SpJm1621.291667SpJm1721.333333SpJm18Thanks youMarta
[[alternative HTML version deleted]]
______________________________________________
R-help at 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.
More information about the R-help
mailing list