0

I would like to add a new row at the bottom of my dataframe, which shows the average of value of each of the columns. For example

A B C D
1 2 3 4
2 3 4 5
3 4 5 6 
4 5 6 7

The result I would like would be:

A    B    C    D
1    2    3    4
2    3    NA   5
3    4    5    NA 
4    5    6    NA
10/4 14/4 14/3 9/2

I have many columns with missing values (NA), also many columns have a different amount of entries, and I have 900 columns.

Is it possible to do this?

Many Thanks

kg23
  • 51
  • 5

2 Answers2

1

It sounds like your data is more like this:

df <- structure(list(Date = c("2020-01-01", "2020-01-02", "2020-01-03", 
"2020-01-04"), A = 1:4, B = 2:5, C = 3:6, D = 4:7), class = "data.frame", 
row.names = c(NA, -4L))

df
#>         Date A B C D
#> 1 2020-01-01 1 2 3 4
#> 2 2020-01-02 2 3 4 5
#> 3 2020-01-03 3 4 5 6
#> 4 2020-01-04 4 5 6 7

In which case, you could do something like

rbind(df, sapply(df, function(x) if(is.numeric(x)) mean(x, na.rm = TRUE) else ""))
#>         Date   A   B   C   D
#> 1 2020-01-01   1   2   3   4
#> 2 2020-01-02   2   3   4   5
#> 3 2020-01-03   3   4   5   6
#> 4 2020-01-04   4   5   6   7
#> 5            2.5 3.5 4.5 5.5

Created on 2020-08-16 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • @kg23 Beware that all columns will be converted into the character type. Replacing `sapply()` with `lapply()` can fix it and be safer. – Darren Tsai Aug 16 '20 at 10:06
0

You can take mean of only numeric column :

library(dplyr)
bind_rows(df, df %>% summarise(across(where(is.numeric), mean, na.rm = TRUE)))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213