I have a column of hours, and I need to group it up into a new column.
structure(list(Q11aWalkHoursEdit.SS = c(0, 1, 2, 3, 4, 5, 6,
6, 7, 2.5, 6, 1.9, 0.5)),
.Names = "Q11aWalkHoursEdit.SS",
class = "data.frame",
row.names = c(NA, -13L))
I initially thought I'd use if, as follows:
grouphours <- function(data){
data$Q11aWalkHours.SS <- NA
if(data["Q11aWalkHoursEdit.SS"] >= 5){
data["Q11aWalkHours.SS"] = "5 hours +"
} else if(data["Q11aWalkHoursEdit.SS"] > 4){
data["Q11aWalkHours.SS"] = "4-5 hours"
} else if(data["Q11aWalkHoursEdit.SS"] > 3){
data["Q11aWalkHours.SS"] = "3-4 hours"
} else if(data["Q11aWalkHoursEdit.SS"] > 2){
data["Q11aWalkHours.SS"] = "2-3 hours"
} else if(data["Q11aWalkHoursEdit.SS"] > 1){
data["Q11aWalkHours.SS"] = "1-2 hours"
} else if(data["Q11aWalkHoursEdit.SS"] > 0){
data["Q11aWalkHours.SS"] = "0-1 hours"
} else if(data["Q11aWalkHoursEdit.SS"] == 0){
data["Q11aWalkHours.SS"] = "0 hours"
} else {
data["Q11aWalkHours.SS"] = NA
}
return(data)
}
test <- grouphours(stuff)
But this doesn't work, because if doesn't work on vectors. It gives the following error:
1: In if (data["Q11aWalkHoursEdit.SS"] >= 5) { ... :
the condition has length > 1 and only the first element will be used
I then started writing a for loop, which also doesn't work, and I don't really understand why:
grouphours <- function(data){
data$Q11aWalkHours.SS <- NA
l<-length(stuff$Q11aWalkHoursEdit.SS)
for(i in 1:l){
if(data["Q11aWalkHoursEdit.SS"] >= 5){
data["Q11aWalkHours.SS"] = "5 hours +"
} else if(data["Q11aWalkHoursEdit.SS"] > 4){
data["Q11aWalkHours.SS"] = "4-5 hours"
} else if(data["Q11aWalkHoursEdit.SS"] > 3){
data["Q11aWalkHours.SS"] = "3-4 hours"
} else if(data["Q11aWalkHoursEdit.SS"] > 2){
data["Q11aWalkHours.SS"] = "2-3 hours"
} else if(data["Q11aWalkHoursEdit.SS"] > 1){
data["Q11aWalkHours.SS"] = "1-2 hours"
} else if(data["Q11aWalkHoursEdit.SS"] > 0){
data["Q11aWalkHours.SS"] = "0-1 hours"
} else if(data["Q11aWalkHoursEdit.SS"] == 0){
data["Q11aWalkHours.SS"] = "0 hours"
} else {
data["Q11aWalkHours.SS"] = NA
}
}
return(data)
}
test <- grouphours(stuff)
Regardless, I feel like I shouldn't need to use a loop - based on comments in this question: Warning "the condition has length > 1 and only the first element will be used" But I'm having trouble understanding how to apply those responses to my situation.
EDIT:
Thank you for helping everyone. I used the following code which made it work perfectly. I think cut
would be a 'better' solution, but I don't understand it yet so will use ifelse for now.
stuff$test <- ifelse(stuff$Q11aWalkHoursEdit.SS>=5, "Five +",
ifelse (stuff$Q11aWalkHoursEdit.SS>=4, "Four to five",
ifelse (stuff$Q11aWalkHoursEdit.SS>=3, "Three to four",
ifelse (stuff$Q11aWalkHoursEdit.SS>=2, "Two to three",
ifelse (stuff$Q11aWalkHoursEdit.SS>=1, "One to two",
ifelse(stuff$Q11aWalkHoursEdit.SS>0, "0 to one", "Zero")
)
)
)
)
)