You are calling mutate
with a matrix
, but mutate
needs a data.frame
.
Maybe you use rbind
instead of cbind
and t
and convert the matrix
to a data.frame
, that mutate
can work with it.
do.call(rbind, lapply(df, summary)) %>%
as.data.frame %>% mutate(Interquatrile = `3rd Qu.` - `1st Qu.`)
# Min. 1st Qu. Median Mean 3rd Qu. Max. Interquatrile
#time 23 806 1976 1669.95587 2364 3329 1558
#age 18 53 61 59.75457 69 85 16
Or your code by using in addition as.data.frame
.
do.call(cbind, lapply(df, summary)) %>%
t() %>% as.data.frame %>% mutate(Interquatrile = `3rd Qu.` - `1st Qu.`)
# Min. 1st Qu. Median Mean 3rd Qu. Max. Interquatrile
#time 23 806 1976 1669.95587 2364 3329 1558
#age 18 53 61 59.75457 69 85 16
Or skip the conversion to data.frame
and cbind
Interquatrile to the matrix.
do.call(rbind, lapply(df, summary)) %>%
cbind(., Interquatrile = .[,"3rd Qu."] - .[,"1st Qu."])
# Min. 1st Qu. Median Mean 3rd Qu. Max. Interquatrile
#time 23 806 1976 1669.95587 2364 3329 1558
#age 18 53 61 59.75457 69 85 16
Or do it directly in the function called by lapply
and get also a matrix
, which could be converted to a data.frame
if needed.
do.call(rbind,
lapply(df, function(x) {
y <- summary(x)
c(y, Interquatrile = y[["3rd Qu."]] - y[["1st Qu."]])} ))
# Min. 1st Qu. Median Mean 3rd Qu. Max. Interquatrile
#time 23 806 1976 1669.95587 2364 3329 1558
#age 18 53 61 59.75457 69 85 16