refactor data.frame column values

后端 未结 3 529
小蘑菇
小蘑菇 2021-01-16 20:53

Sorry guys if this is a noob question. I need help on how to loop over my dataframe.Here is a sample data.

a <- c(10:29);
b <- c(40:59);
e <- rep(1,         


        
3条回答
  •  再見小時候
    2021-01-16 21:11

    I would use cut() for this:

    test$e = cut(test$a, 
                 breaks = c(0, 15, 20, 25, 30), 
                 labels = c(1, 2, 3, 4))
    

    If you want to "generalize" the cut--in other words, where you don't know exactly how many sets of 5 (levels) you need to make--you can take a two-step approach using c() and seq():

    test$e = cut(test$a, 
                 breaks = c(0, seq(from = 15, to = max(test$a)+5, by = 5)))
    levels(test$e) = 1:length(levels(test$e))
    

    Since Backlin beat me to the cut() solution, here's another option (which I don't prefer in this case, but am posting just to demonstrate the many options available in R).

    Use recode() from the car package.

    require(car)    
    test$e = recode(test$a, "0:15 = 1; 15:20 = 2; 20:25 = 3; 25:30 = 4")
    

提交回复
热议问题