Reputation: 25
I am trying to loop through a dataframe column that has commentary and have each commentary appear in a Yes/No box where if the user selects yes then it writes "It is Ok" and if the user selects no then it writes "Needs Investigation" in a new column in that dataframe. And loop through each one and do the same.
The problem is that the loop is saving the last Yes or No selection to every row. For example if my last Yes or No box, I select Yes, then it goes and puts "It is OK" to the new column in every row. I want it to do it for each row individually.
I think I have a logic problem going through the loop, please help.
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,])
Upvotes: 0
Views: 37
Reputation: 1268
It looks like you're not using an index to assign the values, so it's updating the entire column every time. Try this:
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,])
Upvotes: 2