我正在尝试遍历具有注释的数据框列,并让每个注释显示在“是/否”框中,如果用户选择“是”,则其写为“ It is Ok”,如果用户选择“否”,则其写为“需要调查”在该数据框中的新列中。并循环遍历每个,并执行相同的操作。
问题在于循环将最后的是或否选择保存到每一行。例如,如果我最后一个“是”或“否”框选择了“是”,则它将“确定”添加到每一行的新列中。我希望它单独对每一行进行处理。
我认为我在循环中遇到逻辑问题,请帮忙。
for(comments in commentsAMFdata$Commentary.for.variances.x) {
msgBox <- tkmessageBox(title = "Is AMF Commentary accurate?", message =
comments, icon ="info", type ="yesno")
msgBoxCharacter <- as.character(msgBox)
if(msgBoxCharacter == "no") {
commentsAMFdata$commentupdate <- c("Needs Investigation")
} else {
commentsAMFdata$commentupdate <- c("It is OK")
}
}
errorlogcomments <- which(commentsAMFdata$commentupdate != "It is OK")
errorlogcommentdf <- data.frame(commentsAMFdata[errorlogcomments,])
答案 0 :(得分:2)
您似乎没有使用索引来分配值,因此每次都会更新整个列。试试这个:
for(i in seq_along(commentsAMFdata$Commentary.for.variances.x)) {
msgBox <- tkmessageBox(title = "Is AMF Commentary accurate?",
message = commentsAMFdata$Commentary.for.variances.x[i],
icon = "info",
type = "yesno")
msgBoxCharacter <- as.character(msgBox)
if(msgBoxCharacter == "no") {
commentsAMFdata$commentupdate[i] <- c("Needs Investigation")
} else {
commentsAMFdata$commentupdate[i] <- c("It is OK")
}
}
errorlogcomments <- which(commentsAMFdata$commentupdate != "It is OK")
errorlogcommentdf <- data.frame(commentsAMFdata[errorlogcomments,])