简体   繁体   中英

Aggregate function in R not assigning to df correctly

I have a dataframe in R with columns named ag where cols a and b are non-numeric and the rest are numeric.

When I run the following line in the console, it works as intended - giving me the standard deviation, n, and mean of each of the variables:

df %>% 
select(a, b, c, d, e) %>%
aggregate(.~a+b, data = ., FUN = function(x) c(avg = mean(x), std = sd(x, na.rm = TRUE), n = length(x)))

However, when I try and assign the output to a dataframe, it only runs the mean function and doesn't create the columns for standard deviation or n. Why does this happen?

As we are using the dplyr the group_by and summarise/mutate can get the expected output

library(dplyr)
df %>% 
   select(a, b, c, d, e) %>%
   group_by(a, b) %>%
   mutate(n = n()) %>%
   group_by(n, add = TRUE) %>%
   summarise_all(funs(mean, sd)) 

Regarding why the aggregate is behaving differently, we are concatenating the output of two or more function and it returns a single column with matrix output for 'c', 'd' and 'e'.

str(res)
#'data.frame':   5 obs. of  5 variables:
# $ a: Factor w/ 3 levels "A","B","C": 1 3 1 2 3
# $ b: Factor w/ 2 levels "a","b": 1 1 2 2 2
# $ c: num [1:5, 1:3] -0.495 0.131 0.448 -0.495 -0.3 ...
#  ..- attr(*, "dimnames")=List of 2
#  .. ..$ : NULL
#  .. ..$ : chr  "avg" "std" "n"
# $ d: num [1:5, 1:3] -0.713 1.868 -0.71 -0.508 -0.545 ...
#  ..- attr(*, "dimnames")=List of 2
#  .. ..$ : NULL
#  .. ..$ : chr  "avg" "std" "n"
# $ e: num [1:5, 1:3] -0.893 -0.546 -0.421 1.572 -0.867 ...
#  ..- attr(*, "dimnames")=List of 2
#  .. ..$ : NULL
#  .. ..$ : chr  "avg" "std" "n"

where res is the output from the OP's code

In order to convert it to normal data.frame columns, use

res1 <- do.call(data.frame, res)
str(res1)
#'data.frame':   5 obs. of  11 variables:
# $ a    : Factor w/ 3 levels "A","B","C": 1 3 1 2 3
# $ b    : Factor w/ 2 levels "a","b": 1 1 2 2 2
# $ c.avg: num  -0.495 0.131 0.448 -0.495 -0.3
# $ c.std: num  0.233 NA NA 1.589 1.116
# $ c.n  : num  2 1 1 3 2
# $ d.avg: num  -0.713 1.868 -0.71 -0.508 -0.545
# $ d.std: num  1.365 NA NA 0.727 0.322
# $ d.n  : num  2 1 1 3 2
# $ e.avg: num  -0.893 -0.546 -0.421 1.572 -0.867
# $ e.std: num  0.771 NA NA 1.371 0.255
# $ e.n  : num  2 1 1 3 2

data

set.seed(24)
df <- data.frame(a = rep(LETTERS[1:3], each = 3), 
   b = sample(letters[1:2], 9, replace = TRUE), 
   c = rnorm(9), d = rnorm(9), e = rnorm(9))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM