I have a data frame with some identical names in it, and wish to get some basic stats on some associated numbers. Originally, the data look like this:
name res1 res2 res3
foo 1.1 NA 10
foo 2.4 NA 2.1
foo NA 1 1.2
bar 1 2 3
In order to get stats, I was thinking of nesting the data like this:
name res1 res2 res3
foo [1.1,2.4] [1] [10,2.1,1.2]
bar [1] [2] [3]
This would be a step doing things like getting the mean and S.D, etc. of each vector. i.e. I would then add columns for the mean of res1, the SD of res1, etc. How can I nest my data frame based on identical values in a column?
While I agree with thelatemail's suggestion, you can get the desired output with dplyr
:
library(dplyr)
dtf %>% group_by(name) %>% summarize_all( ~ list(.[!is.na(.)]))
# # A tibble: 2 x 4
# name res1 res2 res3
# <fctr> <list> <list> <list>
# 1 bar <dbl [1]> <int [1]> <dbl [1]>
# 2 foo <dbl [2]> <int [1]> <dbl [3]>
the data:
dtf <- read.table(textConnection('name res1 res2 res3
foo 1.1 NA 10
foo 2.4 NA 2.1
foo NA 1 1.2
bar 1 2 3'), header = TRUE)