我有一个替换r的问题,我有一个变量,有许多值,我想在子串中替换" fin",我想用小于26的频率替换值,这里和#39;我的代码:
n.tab <- table(as.character( joined1$codigo) )
n.many <- names( n.tab[ n.tab < 26] )
length(n.many)#2311 NOTE: fin is made with paste of values, codigo is one of them
joinedAux<-joined #saving a copy
for (j in 1:length(n.many)) {#walk through elements of n.many
joinedAux$fin<-gsub(as.character(n.many[j]), "other", as.character(joined$fin))#substitute (not working)
}
它伴随着这个警告:&#34;要替换的项目数量不是替换项目长度的倍数&#34;
当我观看&#34;加入Aux $ fin&#34;时,它什么也没做。它和#34;加入$ fin&#34;是一样的。这是一个例子:
Joined$codigo Joined$fin
12 12 valid high
22 22 wrong un
23 23 wrong in
13 13 valid high
15 15 valid very high
替换后我需要它:
Joined$codigo Joined$fin
12 other valid high #codigo in n.many
22 22 wrong un
23 other wrong in #codigo in n.many
13 other valid high #codigo in n.many
15 15 valid very high
提前致谢!
答案 0 :(得分:1)
如果我正确理解您的问题,您需要遍历data.frame而不是字符串子集。
joinedAux$fin <- sapply(1:nrow(joinedAux), function(j) if(joined1$codigo[j] %in% n.many) gsub(joined1$codigo[j],'other',joined1$codigo[j]) else joined1$codigo[j])
如果你对sapply感到不舒服,for循环也会起作用:
fin <- NULL
for(j in 1:nrow(joinedAux)) fin[j] <- if(joined1$codigo[j] %in% n.many) gsub(joined1$codigo[j],'other',joined1$codigo[j]) else joined1$codigo[j])
如果你乐于避免gsub(它不适用于向量),最简单的方法可能是:
joinedAux$fin <- joined1$codigo
joinedAux$fin[which(joined1$codigo %in% n.many)] <- 'other'