R 对每行不同范围的列求和
我想在我的数据集中创建一个新变量,该变量对特定范围的列求和,但是每行要求和的列的范围不同 这是我的示例数据集。我想创建一个新变量,R 对每行不同范围的列求和,r,loops,for-loop,sum,row,R,Loops,For Loop,Sum,Row,我想在我的数据集中创建一个新变量,该变量对特定范围的列求和,但是每行要求和的列的范围不同 这是我的示例数据集。我想创建一个新变量,NUM1,它是列1到ARRAYEND的总和,其中变量ARRAYEND指定列NUM1的总和 #create sample dataset r1<-c(1,1,1,1,0,2) r2<-c(1,1,0,1,1,4) df<-as.data.frame(rbind(r1,r2)) names(df)<-c("Flag1",&quo
NUM1
,它是列1到ARRAYEND
的总和,其中变量ARRAYEND
指定列NUM1
的总和
#create sample dataset
r1<-c(1,1,1,1,0,2)
r2<-c(1,1,0,1,1,4)
df<-as.data.frame(rbind(r1,r2))
names(df)<-c("Flag1","Flag2","Flag3","Flag4","Flag5","ARRAYEND")
Flag1 Flag2 Flag3 Flag4 Flag5 ARRAYEND
r1 1 1 1 1 0 2
r2 1 1 0 1 1 4
我试图通过dplyr
执行此操作,但它给了我一个错误
df <-df %>% mutate(NUM1 = rowSums(.[1:df$ARRAYEND]))
Warning messages:
1: Problem with `mutate()` input `NUM1`.
ℹ numerical expression has 2 elements: only the first used
ℹ Input `NUM1` is `rowSums(.[1:df$ARRAYEND])`.
2: In 1:df$ARRAYEND :
numerical expression has 2 elements: only the first used
df%变异(NUM1=rowSums([1:df$ARRAYEND]))
警告信息:
1:“mutate()”输入“NUM1”有问题。
ℹ 数值表达式有两个元素:仅使用第一个元素
ℹ 输入'NUM1'是'rowSums([1:df$ARRAYEND])`。
2:在1:df$ARRAYEND中:
数值表达式有两个元素:仅使用第一个元素
Abase R
选项是使用apply
和MARGIN=1
循环行,然后获得seq
ARRAYEND的结果,将“x”的元素子集并获得sum
df$NUM1 <- apply(df, 1, function(x) sum(x[seq(x['ARRAYEND'])]))
注意:tidyverse
不保留行名。因此,最好使用base R
如果我们想要
tidyverse
,一个选项是首先获取作为列的行名
library(tibble)
library(dplyr)
df %>%
rownames_to_column('rn') %>%
rowwise %>%
mutate(NUM1 = sum(c_across(starts_with("Flag"))[seq(ARRAYEND)])) %>%
ungroup %>%
column_to_rownames('rn')
-输出
df
# Flag1 Flag2 Flag3 Flag4 Flag5 ARRAYEND NUM1
#r1 1 1 1 1 0 2 2
#r2 1 1 0 1 1 4 3
# Flag1 Flag2 Flag3 Flag4 Flag5 ARRAYEND NUM1
#r1 1 1 1 1 0 2 2
#r2 1 1 0 1 1 4 3
Flag1 Flag2 Flag3 Flag4 Flag5 ARRAYEND NUM1
r1 1 1 1 1 0 2 2
r2 1 1 0 1 1 4 3
使用
Reduce
transform(
df,
NUM1 = list2DF(Reduce("+", df, accumulate = TRUE))[cbind(seq_along(ARRAYEND), ARRAYEND)]
)
给予
Flag1 Flag2 Flag3 Flag4 Flag5 ARRAYEND NUM1
r1 1 1 1 1 0 2 2
r2 1 1 0 1 1 4 3