in R, selecting rows from mydataframe based upon array of values when array and mydataframe are unequal length

StackOverflow https://stackoverflow.com/questions/18808224

Pergunta

I have the following data called gg and yy.

> str(gg)
 num [1:1992] 128 130 132 185 186 187 188 189 190 191 ...
> str(yy)
'data.frame':   2103 obs. of  2 variables:
 $ grp  : num  128 130 132 185 186 187 188 189 190 191 ...
 $ predd: num  -0.963 -1.518 1.712 -11.286 -8.195 ...
> 

You'll notice that the first several values of gg match the first several from yy.

I would like to select rows from yy if the value yy$grp matches any value in gg. The issue is that gg and yy are of unequal length. Further, there are some values of gg that are not present in yy$grp and also some values of yy$grp not present in gg.

I can't seem to get this to work. It is basically an intersection of the two data sets based upon the index value I mentioned (gg, or yy$grp).

I've tried:

inters<-intersect(gg,yy$grp)
yyint<-yy[yy$grp==inters,]

but get the following

Warning message:
In yy$grp == inters :
  longer object length is not a multiple of shorter object length

> str(yya)
'data.frame':   28 obs. of  2 variables:
 $ grp  : num  128 130 132 185 186 187 188 189 190 191 ...
 $ predd: num  -0.963 -1.518 1.712 -11.286 -8.195 ...

yya should be much longer, according to my plans at least.

Thanks.

Foi útil?

Solução

As I mentioned, I think this is what you want:

yy[yy$grp %in% gg,]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top