Search code examples
rdate-range

Create new column with a specific range of values


I'm trying to create a new column with the range of numbers from 0-40 in the total impressions column. enter image description here

I'm trying to utilize the ifelse function, if that pertains here. This is what I have so far:

group1 <-rocketfuel[rocketfuel$total_impr==<-ifelse(rocketfuel$total_imp<=40, rocketfuel$tot_impr ]

The goal is to break up the customers into 6 group based on number of impressions, so group 1 will be 0-40 impressions, group 2 will be 41-80 impressions and so on.

Update: Utilizing one of the solutions given below, which is almost exactly what I'm looking for

rocketfuel$group1 <- ifelse(rocketfuel$tot_impr<=40,1,NA)

And getting this as a result enter image description here

Instead of the group1 column spitting out the #1, I want it to spit out #21, is that possible?

Figured it out!


Solution

  • Are you trying to subset the values of users who had fewer than 40 impressions or are you trying to create a group of users? Also, make sure your column names are consistent in the call (e.g. "total_imp" vs "total_impr").

    rocketfuel <- data.frame(user_id=c(1069124,1119715,1144181,1435133,1015700),
        test=rep(1,5),
        converted=rep(0,5), 
        tot_impr=c(130,93,21,355,276),
        mode_impr_day=c(1,2,2,2,5), 
        mode_impr_hour=c(20,22,18,10,14))
    

    If you are trying to create a group, just add it as its own variable(column). Using ifelse() is similar to excel, you can do something like this

    rocketfuel$group_lt_40 <- ifelse(rocketfuel$total_impr<=40,1,NA)

    if you're trying to create a column of impressions under 40, you can still use ifelse() and keep the original value of impressions try:

    rocketfuel$group_lt_40_impr <-ifelse(rocketfuel$total_impr<=40,rocketfuel$total_impr,NA)