我有一个数据框
> data.frame(col1=LETTERS[1:4],col2=rep('CDEFGH',4))
col1 col2
1 A CDEFGH
2 B CDEFGH
3 C CDEFGH
4 D CDEFGH
我想制作一个第三列,显示1或0,具体取决于col1中的字符是否在col2中。
我想:
> data.frame(col1=LETTERS[1:4],col2=rep('CDEFGH',4),col3=c(0,0,1,1))
col1 col2 col3
1 A CDEFGH 0
2 B CDEFGH 0
3 C CDEFGH 1
4 D CDEFGH 1
我试过
> grepl(df$col1,df$col2)
[1] FALSE FALSE FALSE FALSE
Warning message:
In grepl(df$col1, df$col2) :
argument 'pattern' has length > 1 and only the first element will be used
和
> grep(df$col1,df$col2)
integer(0)
Warning message:
In grep(df$col1, df$col2) :
argument 'pattern' has length > 1 and only the first element will be used
无济于事。我确信我错过了一些明显的东西,并且会很感激任何想法