简体   繁体   中英

How do I replace column values in an R data frame with new values without doing this...?

I have a data frame with a column labeled "occasion" which ranges from 1 to 21. I would like to subset on any sequence within that range (say: 5 to 19), but then re-sequence the occasion from 5 to 19 to 1 to 15. I know how to subset. It's part 2 I'm stuck on. The only way I know how to do this is:

df$occasion[df$occasion == 5] <- 1 df$occasion[df$occasion == 6] <- 2. . df$occasion[df$occasion == 19] <- 15

Is there a function or index method I can use to simplify this procedure by passing the interval (ie, 5, 19)? Thanks in advance. NARP (Not an R programmer)

If you always want to start with 1, then the following could be an option:

df$occasion <- df$occasion-(min(df$occasion)-1) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM