refactor data.frame column values

后端 未结 3 568
小蘑菇
小蘑菇 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:17

    data.frame(a, b, e=(1:4)[cut(a, c(-Inf, 15, 20, 25, 30))])
    

    Update:

    Greg's comment provides a more direct solution without the need to go via subsetting an integer vector with a factor returned from cut.

    data.frame(a, b, e=findInterval(a, c(-Inf, 15, 20, 25, 30)))
    

提交回复
热议问题