1

I have got these vectors:

a <- c('x','y','z')
b <- c('w','v','s')
c <- c('x','y')
d <- c('s')

and a data.frame which got the names of the vectors as elements:

df <- data.frame(P= c('c','d','c','d'),R = c('a','b','b','a'))
      P R
    1 c a
    2 d b
    3 c b
    4 d a

I want the a data frame look like that:

  P R P_R
1 c a True
2 d b True
3 c b False
4 d a False

I tried the following to reference my elements in df to the objects a,b,c,d)

df$P_R <- is.element((mget(df$P)),(mget(df$R)))

I got:

Fehler in mget(df$P) : ungültiges erstes Argument

Thanks for your help!

Matthias
  • 17
  • 3
  • Like `apply(df, 1, function(x) any(is.element(get(x[1]), get(x[2]))))`? – A5C1D2H2I1M1N2O1R2T1 Feb 25 '17 at 09:42
  • Your error is because "P" and "R" are factors, by the way. If they were character columns, you could probably do something like `mapply(function(x, y) any(is.element(x, y)), mget(df$P), mget(df$R))`. – A5C1D2H2I1M1N2O1R2T1 Feb 25 '17 at 09:44
  • A convenient approach would be to build something off of [this post](http://stackoverflow.com/questions/19891278/r-table-of-interactions-case-with-pets-and-houses) -- `(crossprod(table(stack(list(a = a, b = b, c = c, d = d)))) > 0)[as.matrix(df)]` – alexis_laz Feb 25 '17 at 10:56

0 Answers0