【发布时间】:2018-04-19 18:38:01
【问题描述】:
有一个字符向量(约 35,000 行)(col1),我想根据单独的数据帧 (df1) 对其进行重新编码/重命名。都是字符向量。
col1
C
B
M A
B
R R
C
R R
M A
B
df1:
V1 V2
B blanket
C toy
M A blarg
R R targe
结果是
col1
toy
blanket
blarg
blanket
targe
toy
targe
blarg
blanket
我想做的是说“如果 V1 = col1,替换为 V1 = V2” 我试着按字面意思写:
out<-if(col1==df$V1){replace(df$V1 == df$V2)}
抛出:
Warning message:
In if (testdat == schooldf$V1) { :
the condition has length > 1 and only the first element will be used
我尝试使用 gsub:
out<-gsub(df$V1, df$V2, col1)
抛出:
1: In gsub(schooldf$V1, schooldf$V2, testdat) :
argument 'pattern' has length > 1 and only the first element will be used
2: In gsub(schooldf$V1, schooldf$V2, testdat) :
argument 'replacement' has length > 1 and only the first element will be used
显然,我尝试过的两个论点都存在相似的问题,但我无法弄清楚我做错了什么。
【问题讨论】:
标签: r if-statement gsub