[R] Complex merging problems

npretnar npretnar at gmail.com
Tue Jan 13 07:06:48 CET 2015


Hello,

I have two data frames structured as follows:

df1

v1	v2
1	83
1	83
1	84
1	84	
1	85
1	85
2	90	
2	91
2	91
2	91
2	92
4	89
4	89	
4	90
4	90

df2

v1	v2
1	83
2	83
3	83
4	83
1	84	
2	84
3	84
4	84

... etc.

I am trying to create an indicator variable in df2 to indicate whether the record is identified in df1. I just want to know if it appears once. The problem seems to be that df1 contains multiple records with the same data. I am attempting the following:

df2$v3 <- ifelse(df2$v1 %in% df1$v1 & df2$v2==df2$v1, 1, 0).

However, I get the following warning message:

Warning message:
In df2$v2 == df1$v1 :
  longer object length is not a multiple of shorter object length

Nonetheless, the function outputs all 0's to df2$v3. If anybody has any suggestions with this, I would greatly appreciate it.

Thanks,

- Nick Pretnar
npretnar at gmail.com



More information about the R-help mailing list