我要替换的for循环末尾的长度为零。好的,我知道此错误表示NULL或长度为0的向量。我尝试了一些解决方案,但都无济于事。如何从na / 0结果中转义并继续循环?
for(i in 1:nrow(df)){
df[i,3] <- ... #that doesn't matter
if(length(df[i,3] == 0)){
df[i,3] <- "Not found"
}
}
Error in x[[jj]][iseq] <- vjj : replacement has length zero
或
for(i in 1:nrow(df)){
df[i,3] <- ... #that doesn't matter
if(is.na(df[i,3] == TRUE){
df[i,3] <- "Not found"
}
}
Error in x[[jj]][iseq] <- vjj : replacement has length zero
谢谢。
编辑:示例:
column1 column2 column3
a www.example.com/a 100
b www.example.com/b 150
c www.example.com/c NA
d www.example.com/d NA
以此类推...我的目标是跳过它找不到的链接(c),并保持循环运行。
答案 0 :(得分:1)
为何不使用for循环来转换列,为什么不使用dplyr :: mutate函数?
library(dplyr)
testFrame <- data.frame(a = 1:5, b = c(0,1,1,NA,0))
testFrameEdited <- testFrame %>%
mutate(b = ifelse(b == 0 | is.na(b), 'Not found', b))
testFrameEdited
返回
a b
1 1 Not found
2 2 1
3 3 1
4 4 Not found
5 5 Not found