Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditionally insert rows to a dataframe using dplyr

Tags:

r

dplyr

How can I insert a new row after each row where the hp exceeds 110 in the mtcars dataset, still keeping the cars with hp <=110?

library(tidyverse)
data(mtcars)

I´ve tried the following but without success--

mtcars %>% modify_if(.p = .$hp>110,.f = add_row(.after=1)) #noe equal lengths
mtcars %>% filter(hp>110) %>% add_row(.after=1) #only gives an extra row for the first row meeting condition
mtcars %>% rownames_to_column() %>% 
  group_by(rowname) %>% modify_if(.p=.$hp>110,.f=add_row(.after=1)) #not egual length

the following - using purrr- seems to work:

foo <- function(df){
  if (df$hp>110) {df<-add_row(.data=df,.after=1)}
  df
}

mtcars %>% rownames_to_column(var = "make") %>% nest(-make) %>%
  mutate(new=map(data,~ foo(.x))) %>% select (-data) %>% unnest(new)

Any function called add_row_if ???

like image 527
Misha Avatar asked Apr 15 '17 15:04

Misha


1 Answers

This inserts a new row of random numbers (runif(length(.)) beneath each row that matches your filter:

mtcars %>% filter(hp > 110) %>%
  group_by(row_number()) %>%
  do ( 
    rbind(., runif(length(.)))
    ) %>%
  ungroup()

# # A tibble: 36 × 12
# mpg       cyl        disp          hp       drat          wt        qsec        vs
# <dbl>     <dbl>       <dbl>       <dbl>      <dbl>       <dbl>       <dbl>     <dbl>
#   1  18.70000000 8.0000000 360.0000000 175.0000000 3.15000000 3.440000000 17.02000000 0.0000000
# 2   0.98189043 0.5940565   0.7401747   0.7300069 0.73460416 0.102802673  0.06632293 0.3771534
# 3  14.30000000 8.0000000 360.0000000 245.0000000 3.21000000 3.570000000 15.84000000 0.0000000
# 4   0.02192815 0.4811006   0.6456729   0.2900382 0.69145964 0.741733891  0.34932004 0.1356568
# 5  19.20000000 6.0000000 167.6000000 123.0000000 3.92000000 3.440000000 18.30000000 1.0000000
# 6   0.95568849 0.6532453   0.8744450   0.3346875 0.66217223 0.776532606  0.24731722 0.9633778
# 7  17.80000000 6.0000000 167.6000000 123.0000000 3.92000000 3.440000000 18.90000000 1.0000000
# 8   0.43592634 0.6036127   0.3185138   0.1035780 0.48505561 0.007380369  0.24154177 0.4978268
# 9  16.40000000 8.0000000 275.8000000 180.0000000 3.07000000 4.070000000 17.40000000 0.0000000
# 10  0.10970496 0.9063285   0.4659718   0.2793090 0.04670105 0.337342230  0.85691425 0.2758889
# # ... with 26 more rows, and 4 more variables: am <dbl>, gear <dbl>, carb <dbl>,
# #   `row_number()` <dbl>
like image 130
luffe Avatar answered Oct 22 '22 23:10

luffe