A seemingly simple R task has got me stumped… I have a Number field and a TrueFalse field in a data.frame as seen here.
#Make Data
TrueFalse <- rep(c("TRUE","TRUE", "FALSE"),10)
Number <- seq(1, length(TrueFalse),3)
Table <- as.data.frame(cbind(TrueFalse, Number))
look at the head()
head(Table)
I am trying to create a new column that contains values from the Numbers column if the TrueFalse field is TRUE. In a meta code:
For each row, NewField ==Number if TrueFalse = True, else nothing (ie. Leave as NA)
My working code is below. While searches have been productive, I am still missing a few steps. Thanks in advance for any help!
#for() loop using if()
NewField <- rep("NA", nrow(Table))
for (i in 1:nrow(NewField)){
if(Table$TrueFalse[i] == "TRUE")
{NewField[i] <- Table$Number[i]}
}