[R] simple question on data frames assignment

Michael Artz michaeleartz at gmail.com
Thu Apr 7 13:41:30 CEST 2016


Hi I'm not sure how to ask this, but its a very easy question to answer for
an R person.

What is an easy way to check for a column value and then assigne a new
column a value based on that old column value?

For example, Im doing
 colordata <- data.frame(id = c(1,2,3,4,5), color = c("blue", "red",
"green", "blue", "orange"))
 for (i in 1:nrow(colordata)){
   colordata$response[i] <- ifelse(colordata[i,"color"] == "blue", 1, 0)
 }

which works,  but I don't want to use the for loop I want to "vecotrize"
this.  How would this be implemented?

	[[alternative HTML version deleted]]



More information about the R-help mailing list