更新时间:2023-12-01 12:29:04
dplyr可能有最清晰的解决方案
dplyr may have the cleanest solution to this
使用Len Greski的样本数据如下......
Using Len Greski's sample data below...
data <- data.frame(Age1 = round(runif(100)*100,0))
data%>%
mutate(Age2 = ifelse(between(Age1, 25, 50), "25 - 50 Years",
ifelse(between(Age1, 51, 100),"51 - 100 Years", "Less than 25 years old")))
假设您只想要列的两个值。 ifelse()
对两个以上的比赛效率不高,比如说100。如果不是,我将不得不考虑另一种方法。
Assuming you only want two values for the column. ifelse()
is not efficient for more than two matches, say 100, though. I'll have to think of an alternative approach in the event that its not.
编辑:
或Len建议在这个评论中。
or as Len has suggested below this, in a comment.
data%>%
mutate(Age2 = cut(Age1,c(24,50,100),c("25-50 years","51-100 Years")))