R 脚本错误 {:Dataframe 上需要 TRUE/FALSE 的地方缺少值

R scripting Error { : missing value where TRUE/FALSE needed on Dataframe

我有一个看起来像这样的数据框

Name  Surname  Country  Path
John   Snow      UK     /Home/drive/John 
BOB    Anderson         /Home/drive/BOB
Tim    David     UK     /Home/drive/Tim 
Wayne  Green     UK     /Home/drive/Wayne

我写了一个脚本,它首先检查 country =="UK",如果为真,则使用 R 中的 gsub 将路径从 "/Home/drive/" 更改为 "/Server/files/"

脚本

Pattern<-"/Home/drive/"

Replacement<- "/Server/files/"



 for (i in 1:nrow(gs_catalog_Staging_123))
{

  if( gs_catalog_Staging_123$country[i] == "UK" && !is.na(gs_catalog_Staging_123$country[i]))   
  {
   gs_catalog_Staging_123$Path<- gsub(Pattern , Replacement , gs_catalog_Staging_123$Path,ignore.case=T)
  }

}

我得到的输出:

    Name  Surname  Country  Path
John   Snow      UK     /Server/files/John 
*BOB    Anderson        /Server/files/BOB*
Tim    David     UK     /Server/files/Tim 
Wayne  Green     UK     /Server/files/Wayne

我想要的输出

Name  Surname  Country  Path
    John   Snow      UK     /Server/files/John 
    BOB    Anderson         /Home/drive/BOB
    Tim    David     UK     /Server/files/Tim 
    Wayne  Green     UK     /Server/files/Wayne

我们可以清楚地看到,gsub 无法识别缺失值,也无法追加该行。

许多 R 函数都是矢量化的,因此我们可以避免此处出现循环。

# example data
df <- data.frame(
    name    = c("John", "Bob", "Tim", "Wayne"),
    surname = c("Snow", "Ander", "David", "Green"),
    country = c("UK", "", "UK", "UK"),
    path    = paste0("/Home/drive/", c("John", "Bob", "Tim", "Wayne")),
    stringsAsFactors = FALSE
)

# fix the path
df$newpath <- ifelse(df$country=="UK" & !is.na(df$country), 
                     gsub("/Home/drive/", "/Server/files/", df$path),
                     df$path)

# view result
df
   name surname country              path             newpath
1  John    Snow      UK  /Home/drive/John  /Server/files/John
2   Bob   Ander           /Home/drive/Bob     /Home/drive/Bob
3   Tim   David      UK   /Home/drive/Tim   /Server/files/Tim
4 Wayne   Green      UK /Home/drive/Wayne /Server/files/Wayne

其实这是你代码的问题。每次通过你的循环,你检查行 i 但随后你完全替换了整列。解决方法是在最后一行代码的适当位置添加 [i]

gs_catalog_Staging_123$Path[i] <- gsub(Pattern , Replacement , gs_catalog_Staging_123$Path[i] ,ignore.case=T)