如何在R中平均每两行数据帧

如何在R中平均每两行数据帧,r,dataframe,R,Dataframe,我有以下1000列的数据框: df<- structure(c(1, 2, 2, 1, 2, 2, 2, 1, 3, 3, 2, 2), .Dim = 4:3, .Dimnames = list(c("a", "b", "c", "d"), c("t1", "t2", "t3"))) 一种可能是使用dplyr包。 请注意,我使用的数据与您使用的数据略有不同:在您的数据中,数字实际上是字符值 df <- structure

我有以下1000列的数据框:

df<- structure(c(1, 2, 2, 1, 2, 2, 2, 1, 3, 3, 2, 2), 
              .Dim = 4:3, .Dimnames = list(c("a", "b", "c", "d"), 
               c("t1", "t2", "t3")))

一种可能是使用dplyr包。 请注意,我使用的数据与您使用的数据略有不同:在您的数据中,数字实际上是字符值

df <- structure(c(1, 2, 2, 1, 2, 2, 2, 1, 3, 3, 2, 2), 
               .Dim = 4:3, .Dimnames = list(c("a", "b", "c", "d"), 
                                            c("t1", "t2", "t3")))
首先,我创建包含方法的摘要tibble

    library(dplyr)
    df_summary <- df %>% as_tibble(rownames = "names") %>% 
      group_by(ceiling(1:n() / 2)) %>% 
      summarise(names = paste(names, collapse = "_"),
                t1 = mean(t1),
                t2 = mean(t2),
                t3 = mean(t3)) %>% 
      select(-1)
    # A tibble: 2 x 4
      names    t1    t2    t3
      <chr> <dbl> <dbl> <dbl>
    1 a_b     1.5   2       3
    2 c_d     1.5   1.5     2
然后我将汇总数据与原始数据结合起来:

 df_summary %>% bind_rows(df %>% as_tibble(rownames = "names")) %>% 
  slice(3, 4, 1, 5, 6, 2)
# A tibble: 6 x 4
  names    t1    t2    t3
  <chr> <dbl> <dbl> <dbl>
1 a       1     2       3
2 b       2     2       3
3 a_b     1.5   2       3
4 c       2     2       2
5 d       1     1       2
6 c_d     1.5   1.5     2

一种可能是使用dplyr包。 请注意,我使用的数据与您使用的数据略有不同:在您的数据中,数字实际上是字符值

df <- structure(c(1, 2, 2, 1, 2, 2, 2, 1, 3, 3, 2, 2), 
               .Dim = 4:3, .Dimnames = list(c("a", "b", "c", "d"), 
                                            c("t1", "t2", "t3")))
首先,我创建包含方法的摘要tibble

    library(dplyr)
    df_summary <- df %>% as_tibble(rownames = "names") %>% 
      group_by(ceiling(1:n() / 2)) %>% 
      summarise(names = paste(names, collapse = "_"),
                t1 = mean(t1),
                t2 = mean(t2),
                t3 = mean(t3)) %>% 
      select(-1)
    # A tibble: 2 x 4
      names    t1    t2    t3
      <chr> <dbl> <dbl> <dbl>
    1 a_b     1.5   2       3
    2 c_d     1.5   1.5     2
然后我将汇总数据与原始数据结合起来:

 df_summary %>% bind_rows(df %>% as_tibble(rownames = "names")) %>% 
  slice(3, 4, 1, 5, 6, 2)
# A tibble: 6 x 4
  names    t1    t2    t3
  <chr> <dbl> <dbl> <dbl>
1 a       1     2       3
2 b       2     2       3
3 a_b     1.5   2       3
4 c       2     2       2
5 d       1     1       2
6 c_d     1.5   1.5     2

每两行拆分一次,然后得到每列的平均值,再加上rbind和rbind all

do.call(rbind,
        lapply(seq(1, nrow(df), 2), function(i){
          x <- df[ i:(i + 1), , drop = FALSE]
          res <- rbind(x, colSums(x)/2)
          rownames(res)[ nrow(res) ] <- paste(rownames(x), collapse = "_")
          res
        }))

#      t1  t2 t3
# a   1.0 2.0  3
# b   2.0 2.0  3
# a_b 1.5 2.0  3
# c   2.0 2.0  2
# d   1.0 1.0  2
# c_d 1.5 1.5  2

每两行拆分一次,然后得到每列的平均值,再加上rbind和rbind all

do.call(rbind,
        lapply(seq(1, nrow(df), 2), function(i){
          x <- df[ i:(i + 1), , drop = FALSE]
          res <- rbind(x, colSums(x)/2)
          rownames(res)[ nrow(res) ] <- paste(rownames(x), collapse = "_")
          res
        }))

#      t1  t2 t3
# a   1.0 2.0  3
# b   2.0 2.0  3
# a_b 1.5 2.0  3
# c   2.0 2.0  2
# d   1.0 1.0  2
# c_d 1.5 1.5  2
dplyr的一种可能性是:

df %>%
 data.frame() %>%
 rownames_to_column() %>%
 mutate_if(is.factor, as.numeric) %>%
 group_by(group = gl(n()/2, 2)) %>%
 group_map(~ bind_rows(.x, tibble(rowname = paste(.x$rowname, collapse = "_"), 
                                  t1 = mean(.x$t1),
                                  t2 = mean(.x$t2),
                                  t3 = mean(.x$t3)))) %>%
 ungroup() %>%
 select(-group)

  rowname    t1    t2    t3
  <chr>   <dbl> <dbl> <dbl>
1 a         1     2       2
2 b         2     2       2
3 a_b       1.5   2       2
4 c         2     2       1
5 d         1     1       1
6 c_d       1.5   1.5     1
如果预先将前三行创建为data.frame,名称作为列,因子作为数值变量,则可以省略前三行。然后,它所做的是,首先,使用gl创建一个分组变量。其次,它计算平均值,将名称创建为组中两个元素的组合,并将其与原始数据绑定。最后,它将解组并删除冗余变量。

一种dplyr可能是:

df %>%
 data.frame() %>%
 rownames_to_column() %>%
 mutate_if(is.factor, as.numeric) %>%
 group_by(group = gl(n()/2, 2)) %>%
 group_map(~ bind_rows(.x, tibble(rowname = paste(.x$rowname, collapse = "_"), 
                                  t1 = mean(.x$t1),
                                  t2 = mean(.x$t2),
                                  t3 = mean(.x$t3)))) %>%
 ungroup() %>%
 select(-group)

  rowname    t1    t2    t3
  <chr>   <dbl> <dbl> <dbl>
1 a         1     2       2
2 b         2     2       2
3 a_b       1.5   2       2
4 c         2     2       1
5 d         1     1       1
6 c_d       1.5   1.5     1
如果预先将前三行创建为data.frame,名称作为列,因子作为数值变量,则可以省略前三行。然后,它所做的是,首先,使用gl创建一个分组变量。其次,它计算平均值,将名称创建为组中两个元素的组合,并将其与原始数据绑定。最后,它将解组并删除冗余变量。

另一种dplyr方法。 更新:如果您真的需要行名称a、b、a_b等,请参考我的原始解决方案,以获得可扩展但更复杂的版本

原创的

输出:

# A tibble: 6 x 3
     t1    t2    t3
  <dbl> <dbl> <dbl>
1   1     2       3
2   2     2       3
3   1.5   2       3
4   2     2       2
5   1     1       2
6   1.5   1.5     2
使用行名称更新

输出:

     t1    t2    t3 rn   
  <dbl> <dbl> <dbl> <chr>
1   1     2       3 a    
2   2     2       3 b    
3   1.5   2       3 a_b  
4   2     2       2 c    
5   1     1       2 d    
6   1.5   1.5     2 c_d  
另一种dplyr方法。 更新:如果您真的需要行名称a、b、a_b等,请参考我的原始解决方案,以获得可扩展但更复杂的版本

原创的

输出:

# A tibble: 6 x 3
     t1    t2    t3
  <dbl> <dbl> <dbl>
1   1     2       3
2   2     2       3
3   1.5   2       3
4   2     2       2
5   1     1       2
6   1.5   1.5     2
使用行名称更新

输出:

     t1    t2    t3 rn   
  <dbl> <dbl> <dbl> <chr>
1   1     2       3 a    
2   2     2       3 b    
3   1.5   2       3 a_b  
4   2     2       2 c    
5   1     1       2 d    
6   1.5   1.5     2 c_d  
适用于任意列数的基本R解决方案

M <- matrix(unlist(c(df)), ncol = 2, byrow = TRUE)
M <- cbind(M, rowMeans(M))
M <- matrix(c(t(M)),ncol = ncol(df), byrow = FALSE)

# add row names and column names 
row.names <- matrix(rownames(df), ncol = 2 ,byrow = TRUE)
rownames(M) <- c(t(cbind(row.names, apply(row.names,1, paste, collapse = "_"))))
colnames(M) <- colnames(df)


#        t1   t2   t3
#  a    1.0  2.0    3
#  b    2.0  2.0    3
#  a_b  1.5  2.0    3
#  c    2.0  2.0    2
#  d    1.0  1.0    2
#  c_d  1.5  1.5    2
适用于任意列数的基本R解决方案

M <- matrix(unlist(c(df)), ncol = 2, byrow = TRUE)
M <- cbind(M, rowMeans(M))
M <- matrix(c(t(M)),ncol = ncol(df), byrow = FALSE)

# add row names and column names 
row.names <- matrix(rownames(df), ncol = 2 ,byrow = TRUE)
rownames(M) <- c(t(cbind(row.names, apply(row.names,1, paste, collapse = "_"))))
colnames(M) <- colnames(df)


#        t1   t2   t3
#  a    1.0  2.0    3
#  b    2.0  2.0    3
#  a_b  1.5  2.0    3
#  c    2.0  2.0    2
#  d    1.0  1.0    2
#  c_d  1.5  1.5    2

如果你有1000列呢?那么,我认为,有更好的可能性。现在,我看不出使用这种方法有什么聪明的可能性。如果你有1000列呢?那么,我认为,有更好的可能性。现在,我看不出使用这种方法有什么聪明的可能性。如果你有1000个专栏怎么办?如果你有1000个专栏怎么办?事实上,你需要这个来衡量你在回答的评论中提到的可能有数千个专栏,这是很好的信息,可以添加到你的问题中,所以人们在帮助你解决问题之前知道你的需求是什么?你不能把平均数作为一个单独的矩阵吗?还有,为什么要将数字保留为字符?您当前的示例不是数据框,而是字符矩阵。事实上,您需要它来缩放您在回答的评论中提到的可能有数千列的数字,这是一个很好的信息,可以添加到您的问题中,所以人们在帮助你解决问题之前知道你的需求是什么?你不能把平均数作为一个单独的矩阵吗?还有,为什么要将数字保留为字符?您当前的示例不是数据帧,而是字符矩阵。对于大于4行的情况,是否需要rep1:2,n而不是rep1:n,n?在原文中,我想你们需要的是,对于大于4行的情况,你们需要rep1:2,n而不是rep1:n,n吗?在原始版本中,我认为您需要的是,这是实现OP输出的最佳方法,而对于较大的数据集,这将非常耗时。然而,这是由于期望的输出,这是如此具体和奇怪的我的眼睛。不过你的答案很简洁。干杯。这是实现OP输出的最佳方法,而对于较大的数据集,这将非常耗时。然而,这是由于期望的输出,这是如此具体和奇怪的我的眼睛。不过你的答案很简洁。干杯