Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

r - using case_when directly on a factor

I can't figure something straightforward to do this simple thing :

I have a variable age_cl which is a factor object with age as classes.

If I want to merge two levels I'll just go easily with a forcats::fct_collapse.

However, now I would like to split one level in two different levels (e.g. the "25-34" level into a "25-29" and a "30-34" using the initial integer age variable to conditionally split).

I couldn't find any equivalent in forcats to do so, hence I'd like to use a standard case_when but it only takes character vector as input...

Anyone with any thoughts on this ?

Thanks a lot

question from:https://stackoverflow.com/questions/65952962/using-case-when-directly-on-a-factor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you considered cut ? You can put custom breaks and labels for it.

breaks <- c(24, 29, 34)
labels <- paste(breaks[-length(breaks)] + 1, breaks[-1], sep = '-')
labels
#[1] "25-29" "30-34"

transform(df, age_cl = cut(age, breaks, labels))

#   age age_cl age_cl_expected
#1   25  25-29           25-29
#2   26  25-29           25-29
#3   27  25-29           25-29
#4   28  25-29           25-29
#5   29  25-29           25-29
#6   30  30-34           30-34
#7   31  30-34           30-34
#8   32  30-34           30-34
#9   33  30-34           30-34
#10  34  30-34           30-34

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...