[R] select groups

arun smartpink111 at yahoo.com
Wed Feb 12 06:40:05 CET 2014



Hi,
Try:

row.names(res) <- gsub(".*\\.","",row.names(res))
 row.names(res)[1:2]
#[1] "23" "2" 

A.K.


Thank you very much. 

But it's possible have the inicial number of the row without 
a.23, but only 23, for example? It's important to know which row I had 
select. 

Thank you very much for your help. 



On Tuesday, February 11, 2014 12:21 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,
Try:

set.seed(42)
 dat <-as.data.frame(matrix(sample(20:100,4*45,replace=TRUE),ncol=4))
set.seed(345)
 dat <- within(dat,class1 <- sample(letters[1:3],45,replace=TRUE) )
 table(dat$class1)*0.4
#
#  a   b   c 
#6.0 4.8 7.2 
set.seed(85)
res <- do.call(rbind,lapply(split(dat,dat$class1),function(x) x[sample(nrow(x),round(0.4*nrow(x)),replace=FALSE),]))
table(res$class1)

#a b c 
#6 5 7 

 row.names(res) <- 1:nrow(res)

res

A.K.


Hi, 

I have now a new question. Suppose that we have the data frame 

V1    V2    V3     V4       class 
23    32     65     33        a 
15    54     76     98        b 
21    23     98     23        a 
23    32     65     33        c 
15    54     76     98        b 
21    23     98     23        c 
23    32     65     33        a 
15    54     76     98        b 
21    23     98     23        c 
... 
and I need to select 40% (for example) to each class (consider that we have a lot of rows). 

Thanks




More information about the R-help mailing list