首页 文章

R dplyr rowMeans with filter

提问于
浏览
0

我看过几篇关于将rowMeans类型的结果变为mutate的帖子 . 例如和dplyr - using mutate() like rowmeans() - 但我想让另一个变量充当过滤器 .

据我所知,这些数据并不整齐,“f#”和“d#”变量可以重新整形,然后转换为“f”和“d”,然后过滤“f”并汇总“d” . 但有没有办法在不重塑的情况下做到这一点?我设计了下面的代码

library(tidyverse)

x<-data.frame(f1=c(1,1), f2=c(1,0), f3=c(1,1),
              d1=c(3,2), d2=c(4,8), d3=c(8,16))
x

x %>%
  rowwise() %>%
  mutate(agg=sum(f1*d1, f2*d2, f3*d3) / sum(f1, f2, f3) )

#Source: local data frame [2 x 7]
#Groups: <by row>

# A tibble: 2 x 7
#     f1    f2    f3    d1    d2    d3   agg
#  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1  1.00  1.00  1.00  3.00  4.00  8.00  5.00
#2  1.00  0     1.00  2.00  8.00 16.0   9.00

但是,当有很多变量时,我失去了使用范围的能力,所以我不能说“f1 * d1”:“f2 * d2” - 是否有更通用的方法?

1 回答

  • 1

    假设 f 列和 d 列具有相同的后缀且长度相等,即 f 列和 d 列的数量相同,则可以使用select辅助函数:

    x %>% 
        select(sort(names(x))) %>%   # sort the names so the f columns and d columns correspond
        mutate(agg = {
            fs = select(., starts_with('f')) 
            ds = select(., starts_with('d'))
            rowSums(fs * ds) / rowSums(fs) 
        })
    
    #  d1 d2 d3 f1 f2 f3 agg
    #1  3  4  8  1  1  1   5
    #2  2  8 16  1  0  1   9
    

相关问题