R-如何根据包含要用于操作的列名称的额外列对每行的不同列进行操作

  • 本文关键字:操作 包含 用于 何根 r select subset mean
  • 更新时间 :
  • 英文 :


我是R的新手。我想计算数据帧每行的平均值,但每行使用不同的列子集。我有两个额外的列,它们为我提供了表示";"开始";以及";结束";我应该分别用来计算每个平均值。

让我们以为例

dframe <- data.frame(a=c("2","3","4", "2"), b=c("1","3","6", "2"), c=c("4","5","6", "3"), d=c("4","2","8", "5"), e=c("a", "c", "a", "b"), f=c("c", "d", "d", "c"))
dframe

它提供以下数据帧:

a b c d e f
1 2 1 4 4 a c
2 3 3 5 2 c d
3 4 6 6 8 a d
4 2 2 3 5 b c

列e和f表示我用来计算每行平均值的第一列和最后一列。例如,在第1行,将计算包括a、b、c列((2+1+4(/3->2.3(因此,我想获得以下输出:

a b c d e f mean
1 2 1 4 4 a c  2.3
2 3 3 5 2 c d  3.5
3 4 6 6 8 a d    6
4 2 2 3 5 b c  2.5

我学会了如何创建索引,然后我想使用RowMeans,但我找不到正确的参数。

dframe %>%
mutate(e_indice = match(e, colnames(dframe)))%>%
mutate(f_indice = match(f, colnames(dframe)))%>%
mutate(mean = RowMeans(????, na.rm = TRUE))

非常感谢您的帮助

一个dplyr选项可以是:

dframe %>%
rowwise() %>%
mutate(mean = rowMeans(cur_data()[match(e, names(.)):match(f, names(.))]))
a     b     c     d e     f      mean
<dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl>
1     2     1     4     4 a     c      2.33
2     3     3     5     2 c     d      3.5 
3     4     6     6     8 a     d      6   
4     2     2     3     5 b     c      2.5 

我将定义一个助手函数,让您可以对所需的索引进行切片来自矩阵。

rowSlice <- function(x, start, stop) {
replace(x, col(x) < start | col(x) > stop, NA)
}
rowSlice(matrix(1, 4, 4), c(1, 3, 1, 2), c(3, 4, 4, 3))
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1   NA
#> [2,]   NA   NA    1    1
#> [3,]    1    1    1    1
#> [4,]   NA    1    1   NA

然后使用CCD_ 2选择相关列,并取CCD_ 3。

library(dplyr)
dframe <- data.frame(
a = c(2, 3, 4, 2),
b = c(1, 3, 6, 2),
c = c(4, 5, 6, 3),
d = c(4, 2, 8, 5),
e = c("a", "c", "a", "b"),
f = c("c", "d", "d", "c")
)
dframe %>%
mutate(ei = match(e, colnames(dframe))) %>%
mutate(fi = match(f, colnames(dframe))) %>% 
mutate(
mean = across(a:d) %>%
rowSlice(ei, fi) %>%
rowMeans(na.rm = TRUE)
)
#>   a b c d e f ei fi     mean
#> 1 2 1 4 4 a c  1  3 2.333333
#> 2 3 3 5 2 c d  3  4 3.500000
#> 3 4 6 6 8 a d  1  4 6.000000
#> 4 2 2 3 5 b c  2  3 2.500000

基本R解决方案。首先,将列设置为数字。然后创建一个列列表,在其中应用平均值。然后对所选列应用平均值。

s <- mapply(seq, match(dframe$e, colnames(dframe)), match(dframe$f, colnames(dframe)))
dframe$mean <- lapply(seq(nrow(dframe)), function(x) rowMeans(dframe[x, s[[x]]]))
a b c d e f     mean
1 2 1 4 4 a c 2.333333
2 3 3 5 2 c d      3.5
3 4 6 6 8 a d        6
4 2 2 3 5 b c      2.5

使用apply基本R方法

dframe$mean <- apply(dframe, 1, function(x) 
mean(as.numeric(x[which(names(x) == x["e"]) : which(names(x) == x["f"])])))
dframe
a b c d e f     mean
1 2 1 4 4 a c 2.333333
2 3 3 5 2 c d 3.500000
3 4 6 6 8 a d 6.000000
4 2 2 3 5 b c 2.500000

最新更新