我有3个维度相似的数据帧,结构如下:
> str(Results_first_experiment)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   30 obs. of  8 variables:
 $ Strain: chr  "1" "2" "3" "4" ...
 $ 0.5   : num  3452 4126 2200 3125 1392 ...
 $ 1     : num  11918 14445 7899 11735 5813 ...
 $ 2     : num  19848 20872 16089 19759 13746 ...
 $ 3     : num  20188 19937 20509 21012 19792 ...
 $ 4     : num  16586 17074 15426 14748 15470 ...
 $ 5     : num  16850 17288 17801 14051 17305 ...
 $ 6     : num  12816 14682 16325 15948 16069 ...  
> head (Results_first_experiment)
# A tibble: 6 x 8
  Strain `0.5`    `1`    `2`    `3`    `4`    `5`    `6`
  <chr>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 1      3452. 11918. 19848. 20188. 16586. 16850. 12816 
2 2      4126  14445. 20872. 19937. 17074. 17288  14682.
3 3      2200.  7899. 16089. 20509  15426. 17801  16325.
4 4      3125. 11735. 19758. 21012. 14748  14051. 15948.
5 5      1392.  5813. 13746  19792. 15470. 17305. 16069.
6 6      1501   5769  12730  18339. 17369  18645. 18463.我想检索每个位置的3个数据帧的平均值。我试着去看看How to get mean, median, and other statistics over entire matrix, array or dataframe?。然而,我无法得到每个位置的3个数据帧的平均值
任何帮助都将不胜感激。
发布于 2020-01-07 16:05:50
首先,在数据表或矩阵中,我建议您不要只用数字命名列。如果你在职位上工作,你可以命名为'p.1','pos.1‘等。
当您对对象重复使用data.frames时(此处为您的位置),您可以使用tidyverse轻松处理。下面是一个简单的示例,您可以将其转换为您的数据:
size <- 5
vec.list <- vector("list", size)
position <- paste0("position.", 1:size)
for(i in 1:size){
  a <- runif(5, 0, 1)
  b <- rnorm(5, 2, 4)
  c <- rnorm(5, 0.5, 1)
  vec.list[[i]] <- data.frame(position, a, b, c)
}
vec.list
# unlist to get a data.frame and sort according to position
df.pos <- do.call(rbind.data.frame, vec.list) %>% arrange(position)
# use tidyr::nest() to nest your data by position
pos.nested <- df.pos %>% group_by(position) %>% nest()
# Then use purrr::map() functions to work on nested data
map(.x = pos.nested$data, .f = ~mean(.x$a, na.rm = T))
map(.x = pos.nested$data, .f = ~sd(.x$c, na.rm = T))https://stackoverflow.com/questions/59622149
复制相似问题