我在变量中存储了以下数据框:
project_name task_name
6 TPV
此数据帧存储在名为isPNfound
的变量中。
如果在数据框中查询任务名称,我应该找回项目名称。但是,当项目名称为空时,下面的代码假定可以捕获它并发出警报:
if(isPNfound$project_name == "") {
dataStatus <- paste("Task [", projectTaskName, "] has no Project assigned to it!!!")
print(dataStatus)
chkstatus <- 0
}
如果在数据帧中只有一个与“ TPV”匹配的任务名称,则上述代码有效。但是,假设数据帧中有多行,我需要对其运行一个循环。但是我对如何做到这一点一无所知。
数据框的内容示例:
project_name task_name
6 Rice TPV
7 Beans TPV
8 TPV
我需要以某种方式针对此数据框循环运行我的代码。这是我的尝试:
isPNfound <- dataframeContent
if (length(isPNfound) > 1) {
for (eachtask in isPNfound){
dataStatus <- paste("Task [", projectTaskName, "] has no Project assigned to it!!!")
print(dataStatus)
chkstatus <- 0
}
}
答案 0 :(得分:2)
像这样吗?
chkstatus <- isPNfound$project_name != ""
dataStatus <- ifelse(chkstatus,
"OK",
paste("Task [", isPNfound$task_name, "] has no Project assigned to it!!!")
)
chkstatus <- as.integer(chkstatus)
chkstatus
#[1] 1 1 0
dataStatus
#[1] "OK"
#[2] "OK"
#[3] "Task [ TPV ] has no Project assigned to it!!!"
数据。
isPNfound <-
structure(list(project_name = c("Rice", "Beans", ""),
task_name = c("TPV",
"TPV", "TPV")), row.names = c("6", "7", "8"), class = "data.frame")