drop groups if any of the entries is infinite from dataframe in R

Viewed 58

I have 3 groups for which I have collected data for 12 months for 4 variables for some group * month combination, any of the of variable might have an -Inf value.

temp <- data.frame(group_id = rep(1:3, each = 12),
                 Month = rep(1:12, times = 3),
                 var1 = c(-Inf, rnorm(35)),
                 var2 = rnorm(36),
                 var3 = rnorm(36),
                 var4 = c(rnorm(35), -Inf))

In the above sample data, group 1 and 3 should be dropped and only group 2 should remain.

I have a rather tedious way to achieve it. I look for each of my column whether infinite value is present for any given group and then remove them as shown below. The problem is if I have more than 4 variables, this will become quite lengthy.

library(dplyr)

group_to_keep <- 
temp %>% 
dplyr::group_by(group_id) %>%
dplyr::summarise(var1_inf = sum(is.infinite(var1)),
                 var2_inf = sum(is.infinite(var2)),
                 var3_inf = sum(is.infinite(var3)),
                 var4_inf = sum(is.infinite(var4))) %>%
rowwise %>%
dplyr::mutate(row_sum = sum(var1_inf:var4_inf)) %>%
dplyr::mutate(row_sum = ifelse(row_sum != 0, NA, row_sum)) %>%
tidyr::drop_na()

temp %>% dplyr::filter(group_id %in% group_to_keep$group_id)
1 Answers

One dplyr and purrr option could be:

temp %>%
 group_by(group_id) %>%
 filter(all(pmap_lgl(across(var1:var4), ~ sum(is.infinite(c(...))) == 0)))

   group_id Month    var1    var2    var3    var4
      <int> <int>   <dbl>   <dbl>   <dbl>   <dbl>
 1        2     1 -0.598  -1.92   -0.909  -0.504 
 2        2     2 -0.475  -0.376  -0.0863  0.750 
 3        2     3 -0.653   0.839   0.407  -0.841 
 4        2     4  0.0454 -0.524  -0.0168 -0.0497
 5        2     5  1.03   -0.215   1.09    1.12  
 6        2     6  1.87   -1.12    1.28   -1.78  
 7        2     7  1.46   -1.48    1.08   -1.18  
 8        2     8 -0.462  -0.304  -1.29   -2.02  
 9        2     9 -0.0585 -0.654   0.415   0.892 
10        2    10 -0.229  -1.11   -0.670  -0.696 
11        2    11  1.03    0.0637 -0.718  -0.0300
12        2    12  0.454  -2.14   -0.199   0.142 
Related