是否可以在几行之后添加一个新行,以计算R中每列的平均值?

tkclm6bt  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(92)

我的数据是这样的:

structure(list(ACT = c(23.13958, 23.04186967, 24.20995687, 24.40250502, 
24.04483881, 23.30146732, 24.29608776, 23.39981921), DnaJ = c(22.03439246, 
21.88943566, 22.42216211, 23.16443426, 26.60619229, 27.85283015, 
23.44923267, 23.1289117), `EF-1` = c(22.68375722, 22.30853321, 
22.78175571, 23.60114283, 23.44663203, 24.71161093, 23.12476878, 
23.08036574)), row.names = c("Hairy root_1", "Hairy root_2", 
"Adventitious root_1", "Adventitious root_2", "Embryonic root_1", 
"Embryonic root_2", "Leaf_1", "Leaf_2"), class = "data.frame")

我想在第2、4、6和8行之后创建新行,每一行都是前两行的平均值。
我想要的是:

vaqhlq81

vaqhlq811#

首先,让我们将行名称引入到一个列中,以便dplyr可以轻松地使用它们:

quux <- structure(list(ACT = c(23.13958, 23.04186967, 24.20995687, 24.40250502, 24.04483881, 23.30146732, 24.29608776, 23.39981921), DnaJ = c(22.03439246, 21.88943566, 22.42216211, 23.16443426, 26.60619229, 27.85283015, 23.44923267, 23.1289117), "EF-1" = c(22.68375722, 22.30853321, 22.78175571, 23.60114283, 23.44663203, 24.71161093, 23.12476878, 23.08036574)), row.names = c("Hairy root_1", "Hairy root_2", "Adventitious root_1", "Adventitious root_2", "Embryonic root_1", "Embryonic root_2", "Leaf_1", "Leaf_2" ), class = "data.frame")

library(dplyr)
quux <- quux %>%
  tibble::rownames_to_column() %>%
  tidyr::separate_wider_delim(rowname, delim = "_", names = c("rowname", "num")) %>%
  mutate(num = as.integer(num))
quux
# # A tibble: 8 × 5
#   rowname             num   ACT  DnaJ `EF-1`
#   <chr>             <int> <dbl> <dbl>  <dbl>
# 1 Hairy root            1  23.1  22.0   22.7
# 2 Hairy root            2  23.0  21.9   22.3
# 3 Adventitious root     1  24.2  22.4   22.8
# 4 Adventitious root     2  24.4  23.2   23.6
# 5 Embryonic root        1  24.0  26.6   23.4
# 6 Embryonic root        2  23.3  27.9   24.7
# 7 Leaf                  1  24.3  23.4   23.1
# 8 Leaf                  2  23.4  23.1   23.1

现在,我们可以分组、汇总和追加到原始数据:

out <- quux %>%
  group_by(rowname) %>%
  summarize(num = max(num) + 1L, across(-num, mean)) %>%
  bind_rows(quux) %>%
  arrange(rowname, num)
out
# # A tibble: 12 × 5
#    rowname             num   ACT  DnaJ `EF-1`
#    <chr>             <int> <dbl> <dbl>  <dbl>
#  1 Adventitious root     1  24.2  22.4   22.8
#  2 Adventitious root     2  24.4  23.2   23.6
#  3 Adventitious root     3  24.3  22.8   23.2
#  4 Embryonic root        1  24.0  26.6   23.4
#  5 Embryonic root        2  23.3  27.9   24.7
#  6 Embryonic root        3  23.7  27.2   24.1
#  7 Hairy root            1  23.1  22.0   22.7
#  8 Hairy root            2  23.0  21.9   22.3
#  9 Hairy root            3  23.1  22.0   22.5
# 10 Leaf                  1  24.3  23.4   23.1
# 11 Leaf                  2  23.4  23.1   23.1
# 12 Leaf                  3  23.8  23.3   23.1

如果你想重新组合rownamenum,并选择性地返回到行名称(顺便说一句,dplyr通常不保留),我们可以这样做:

out %>%
  unite(rowname, rowname, num, sep = "_") %>%
  tibble::column_to_rownames("rowname")
#                          ACT     DnaJ     EF-1
# Adventitious root_1 24.20996 22.42216 22.78176
# Adventitious root_2 24.40251 23.16443 23.60114
# Adventitious root_3 24.30623 22.79330 23.19145
# Embryonic root_1    24.04484 26.60619 23.44663
# Embryonic root_2    23.30147 27.85283 24.71161
# Embryonic root_3    23.67315 27.22951 24.07912
# Hairy root_1        23.13958 22.03439 22.68376
# Hairy root_2        23.04187 21.88944 22.30853
# Hairy root_3        23.09072 21.96191 22.49615
# Leaf_1              24.29609 23.44923 23.12477
# Leaf_2              23.39982 23.12891 23.08037
# Leaf_3              23.84795 23.28907 23.10257
k3fezbri

k3fezbri2#

使用lapplysplitbase R方法

Reduce(rbind, 
  lapply(split(df, sub("_.*", "", unique(rownames(df)))), function(x){
    res <- rbind(x, colMeans(x))
    rownames(res)[3] <- paste0(unique(sub("_.*", "", rownames(x))), "_3")
    res}))
                         ACT     DnaJ     EF-1
Adventitious root_1 24.20996 22.42216 22.78176
Adventitious root_2 24.40251 23.16443 23.60114
Adventitious root_3 24.30623 22.79330 23.19145
Embryonic root_1    24.04484 26.60619 23.44663
Embryonic root_2    23.30147 27.85283 24.71161
Embryonic root_3    23.67315 27.22951 24.07912
Hairy root_1        23.13958 22.03439 22.68376
Hairy root_2        23.04187 21.88944 22.30853
Hairy root_3        23.09072 21.96191 22.49615
Leaf_1              24.29609 23.44923 23.12477
Leaf_2              23.39982 23.12891 23.08037
Leaf_3              23.84795 23.28907 23.10257

相关问题