我有两个专栏。
每当第一列具有给定行的值"Breast"时,我希望添加另一列(test$IHC)的值。
这段代码应该可以正常工作:
testdat$Pathology[testdat$Pathology == "Breast"] <- paste("Breast (", testdat$IHC, ")")但是,问题是函数testdat$Pathology[testdat$Pathology == "Breast"]在向量中包含NA,这会在第一行执行函数时提示错误:
NAs are not allowed in subscripted assignments我发现的问题是,na.omit()无法工作,因为我认为它会在数据中产生一些修改:
na.omit(testdat$Pathology[testdat$Pathology == "Breast"]) <- paste("Breast (", testdat$IHC, ")")发布于 2022-05-17 16:56:12
您可以显式地检查NA。请记住,在右侧也要包括相同的过滤器,否则向量将不匹配,并且值将被抵消。
testdat$Pathology[! is.na(testdat$Pathology) & testdat$Pathology == "Breast"] <- paste0("Breast (", testdat$IHC[! is.na(testdat$Pathology) & testdat$Pathology == "Breast"], ")")通过将通用表达式保存在变量中,可以使其更简洁:
subscripts.to.replace <- ! is.na(testdat$Pathology) & testdat$Pathology == "Breast"
testdat$Pathology[subscripts.to.replace] <- paste0("Breast (", testdat$IHC[subscripts.to.replace], ")")https://stackoverflow.com/questions/72277255
复制相似问题