Omry Atia
Omry Atia

Reputation: 2443

cutting interval based on limits in a list

I have the following data frame with 4 numeric columns:

df <- structure(list(a = c(0.494129340746821, 1.0182303327812, 0.412227511922328, 
0.204436644926016, 0.707038309818134, -0.0547300783473556, 1.02124944293185, 
0.381284586356091, 0.375197843213519, -1.18172401075089), b = 
c(-1.34374367808722, 
-0.724644569211516, -0.618107980582741, -1.79274868750102, 
-3.03559838445132, 
-0.205726144151615, -0.441511286334811, 0.126660637747845, 
0.353737902975931, 
-0.26601393471207), c = c(1.36922677098999, -1.81698348029464, 
-0.846111260721092, 0.121256015837603, -1.16499681749603, 1.14145675696301, 
-0.782988942359773, 3.25142254765012, -0.132099541183856, -0.242831877642412
), d = c(-0.30002630673509, -0.507496812070994, -2.59870853299723, 
-1.30109828239028, 1.05029458887117, -0.606381379180569, -0.928822706709913, 
-0.68324741261771, -1.17980245487707, 2.20174180936794)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

I would like to create two new factor columns, in which I group columns 2 and 3 according to the values given in the list L:

ColsToChoose = c(2,3)
L = list()
L[[1]] = c(-0.3, 0.7)
L[[2]] = c(-1, 0.5, 1)

df %>% mutate_at(ColsToChoose, funs(intervals = cut(., c(-Inf, L[[.]], Inf))))

That is, I am expecting to get two new columns, the first called intervals_b indicating if the values of column b (column 2) are between -Inf and -0.3, -0.3 and 0.7 or 0.7 and Inf, and similarly for column c: -Inf to -1, -1 to 0.5, 0.5 to 1 and 1 to Inf.

I am getting an error:

Error in mutate_impl(.data, dots) : Evaluation error: recursive indexing failed at level 2

I would like to do this for the general case, that's why I am using implicit names.

Any ideas?

Upvotes: 1

Views: 197

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389275

You could do this base R mapply passing ColsToChoose of df and L parallely to get the range.

df[paste0("interval", names(df)[ColsToChoose])] <- 
               mapply(function(x, y) cut(x, c(-Inf, y, Inf)), df[ColsToChoose], L)

df

#        a      b      c      d   intervalb   intervalc
#     <dbl>  <dbl>  <dbl>  <dbl>   <chr>       <chr>    
# 1  0.494  -1.34   1.37  -0.300 (-Inf,-0.3] (1, Inf] 
# 2  1.02   -0.725 -1.82  -0.507 (-Inf,-0.3] (-Inf,-1]
# 3  0.412  -0.618 -0.846 -2.60  (-Inf,-0.3] (-1,0.5] 
# 4  0.204  -1.79   0.121 -1.30  (-Inf,-0.3] (-1,0.5] 
# 5  0.707  -3.04  -1.16   1.05  (-Inf,-0.3] (-Inf,-1]
# 6 -0.0547 -0.206  1.14  -0.606 (-0.3,0.7]  (1, Inf] 
# 7  1.02   -0.442 -0.783 -0.929 (-Inf,-0.3] (-1,0.5] 
# 8  0.381   0.127  3.25  -0.683 (-0.3,0.7]  (1, Inf] 
# 9  0.375   0.354 -0.132 -1.18  (-0.3,0.7]  (-1,0.5] 
#10 -1.18   -0.266 -0.243  2.20  (-0.3,0.7]  (-1,0.5] 

A tidyverse approach using same approach

library(tidyverse)

bind_cols(df, 
    map2(df[ColsToChoose], L, ~ cut(.x, c(-Inf, .y, Inf))) %>%
    data.frame() %>%
    rename_all(paste0, "_interval"))

This gives same output as above.

Upvotes: 2

Related Questions