vojtam
vojtam

Reputation: 1255

How to skip character columns when summarise in R?

I have a following problem. I want to compute a mean of two datasets. See bellow:

df1 <- data.frame(year = c(2010, 2011, 2012),
                  month = c("01", "02", "03"),
                  col1 = c(4, 3, 2),
                  col2 = c(23, 41, 32)
)


df2 <- data.frame(year = c(2010, 2011, 2012),
                  month = c("01", "02", "03"),
                  col1 = c(40, 30, 20),
                  col2 = c(13, 11, 12)
)

I tried this:



df_together <- bind_rows(df1 %>% add_rownames(), 
                       df2 %>% add_rownames()) %>% 
  # evaluate following calls for each value in the rowname column
  group_by(rowname) %>% 
  # mean
  summarise_all(mean)

which gives me:

# A tibble: 3 x 5
  rowname  year month  col1  col2
  <chr>   <dbl> <dbl> <dbl> <dbl>
1 1        2010    NA  22      18
2 2        2011    NA  16.5    26
3 3        2012    NA  11      22

Is there a way, how to "skip" characters in column month? I tried to add select_if(is.numeric), but it did not work to me.

Desired output is:

# A tibble: 3 x 5
  rowname  year month  col1  col2
  <chr>   <dbl> <dbl> <dbl> <dbl>
1 1        2010    01  22      18
2 2        2011    02  16.5    26
3 3        2012    03  11      22

Upvotes: 0

Views: 229

Answers (2)

Add the additional grouping variables:

group_by(rowname, year, month) %>% ....

I also recommend you ending the code chunk with

... %>% as.data.frame()

Upvotes: 1

Onyambu
Onyambu

Reputation: 79338

bind_rows(df1, df2) %>% 
   group_by(year, month) %>%
   summarize(across(everything(), mean), .groups = "drop")
# A tibble: 3 x 4
   year month  col1  col2
  <dbl> <chr> <dbl> <dbl>
1  2010 01     22      18
2  2011 02     16.5    26
3  2012 03     11      22

Upvotes: 2

Related Questions