Reputation:
In my current data set, I have a column for state names and then a few other columns with some health information for each state. Basically I want to add a Region column that says which region each state is in. Here's what I've tried so far:
However, I get the error for the shorter object length not being a multiple of the longer object length. I know this means that there is differing length of each vector so it won't apply the argument. I'm essentially trying to say "if state name = one of the states in this item, apply the appropriate region to it". Any help is appreciated.
Upvotes: 0
Views: 50
Reputation: 39613
Try with %in%
as ==
only checks for the first value in your vector:
#Code
cleandata$Region[cleandata$statename %in% south] <- "South"
cleandata$Region[cleandata$statename %in% midwest] <- "Midwest"
Upvotes: 1