R - how to choose a different number of observations in each group

I'm relatively new to r (coming from sas) I need to choose a different number of observations in each group. Groups are identified by the values ​​of two variables

ToSelect <- data.frame(
                           key1=c(1,1,1,1,1,2,2,2,2,2,2,2),
                           key2=c("a","a","b","b","b","a","a","a","a","b","b","b"),
                           var1=c(2,3,4,6,2,7,8,5,7,1,8,5)
                          )
NumObs <- data.frame(
                           key1=c(1,1,2,2),
                           key2=c("a","b","a","b"),
                           NumObs=c(1,2,2,1)
                       )

I tried (from the question "Choose the first 80 observations for each level in R")

ToSelect <- merge(x=ToSelect,y=NumObs,by=c("key1","key2"))
library(plyr)
Selected <- ddply(ToSelect, .(key1,key2), head, n = NumObs)

which gives

Error: length (n) == 1L not TRUE

which is probably an obvious mistake for experts (n scalar, NumObs vector?)

From the same question, I tried:

Selected <- do.call(
                     rbind, 
                     lapply(split(ToSelect, c(ToSelect$key1,ToSelect$key2)), head, NumObs)
                    )

which gives

Error: length (n) == 1L is not TRUE. Additionally: Warning message: In split.default (x = seq_len (nrow (x)), f = f, drop = drop, ...): data length is not a multiple of the split variable

So, the same error as before, plus a few things, I can not use split if the groups have different lengths?

" ", rle/sequence , ddply:

ToSelect <- ddply(ToSelect, .(key1, key2), function(z){
                                                         cbind(var1=z$var1,NumObs=z$NumObs,
                                                         data.frame(
                                                                       SeqNum = seq_along(z$key2)
                                                                    )
                                                               )
                                                       }
                 )
Selected <- ToSelect[ToSelect$SeqNum<=ToSelect$NumObs,c("key1","key2","var1")]

.

, , ? !

+4
3

, data.table. :

#Convert objects to data.table
require("data.table")
ToSelect <- data.table(ToSelect)
NumObs <- data.table(NumObs)

#Merge data
ToSelect <- merge(ToSelect,NumObs,by=c("key1","key2"),all.x=T)

#Provide intra-group ordering variable
ToSelect[,Grp.Seq:=seq(1:.N),by=c("key1","key2")]
Selected <- ToSelect[NumObs>=Grp.Seq]
Selected

   key1 key2 var1 NumObs Grp.Seq
1:    1    a    2      1       1
2:    1    b    4      2       1
3:    1    b    6      2       2
4:    2    a    7      2       1
5:    2    a    8      2       2
6:    2    b    1      1       1

R, , , data.table . , data.frame . data.frame data.table, .

+2

, , , , data.table . var1 , NumObs. :

# Load package
require(data.table)
# Make your data.frames into data.tables                       
ts <- data.table( ToSelect , key = c( "key1","key2"))
no <- data.table( NumObs , key = c( "key1","key2") )

# Join together based on key columns and sample by group
no[ts][ , sample( var1 , NumObs , TRUE ) , by = c("key1","key2") ]
#   key1 key2 V1
#1:    1    a  2 
#2:    1    b  6 #|_ Two observations of group 1b
#3:    1    b  6 #|
#4:    2    a  5 #|_ Two observations of group 2a
#5:    2    a  8 #|
#6:    2    b  5

( , 1b - ), TRUE sample ( , ).

+1

, n ToSelect key1 key2 -pair. n NumObs NumObs data.frame.

dplyr, .

:

require(dplyr)

(= ) data.frames :

df <- left_join(ToSelect, NumObs, by=c("key1", "key2"))

data.frame df key1 key2 NumObs oberservations :

df <- df %.% group_by(key1, key2) %.% filter(1:n() <= NumObs)

>df
#  key1 key2 var1 NumObs
#1    1    a    2      1
#2    1    b    4      2
#3    1    b    6      2
#4    2    a    7      2
#5    2    a    8      2
#6    2    b    1      1

NumObs, :

df <- df %.% group_by(key1, key2) %.% filter(1:n() <= NumObs) %.% select(-NumObs)
0
source

Source: https://habr.com/ru/post/1541247/


All Articles