R for循环计算行平均值的比率
R for loop to compute ratio of row means
我有一个这样的数据框 (df):
Rif A1 A2 A3 A4 A5 B1 B2 B3 B4 B5
a1 1 1 0 3 7 10 10 7 7 7
b1 3 3 3 0 0 5 5 5 5 0
c1 1 3 0 0 2 3 3 3 3 3
我需要按如下方式计算 V 列表:
V1: mean(A5:A5)/mean(B5:B5)
V2: mean (A4:A5) / mean (B4:B5)
V3: mean (A3:A5) / mean (B3:B5)
....
我想我应该使用 for 循环。我试过下面写的循环,但它不起作用:
ref <- c(1, 2, 3, 4, 5)
dat1 <- NULL
for (i in 2:length(ref)) {
for (j in 1:nrow(df)) {
dat1[j] <- rowMeans(df[j,i-1:6])/rowMeans(df[j,i+6-1:11])
}
df[,paste0("V", i)] <- dat1
}
我期望的输出是这样的:
Rif A1 A2 A3 A4 A5 B1 B2 B3 B4 B5 V1 V2 V3 V4 V5
a1 1 1 0 3 7 10 10 7 7 7
b1 3 3 3 0 0 5 5 5 5 0
c1 1 3 0 0 2 3 3 3 3 3
继续你的步骤,你可以试试:
for (i in 1:5) {
df[,paste0("V", i)] <- rowMeans(df[,(i+1):6,drop=FALSE])/rowMeans(df[,(i+6):11,drop=FALSE])
}
df
# Rif A1 A2 A3 A4 A5 B1 B2 B3 B4 B5 V1 V2 V3 V4 V5
#1 a1 1 1 0 3 7 10 10 7 7 7 0.2926829 0.3548387 0.4761905 0.7142857 1.0000000
#2 b1 3 3 3 0 0 5 5 5 5 0 0.4500000 0.4000000 0.3000000 0.0000000 NaN
#3 c1 1 3 0 0 2 3 3 3 3 3 0.4000000 0.4166667 0.2222222 0.3333333 0.6666667
您可以跳过 j
- 循环,这是每行 sub-setting 然后使用 rowMeans
。另一方面,当您仅对一列进行子集化时,需要 drop=FALSE
。索引目前是硬编码的,但在数据结构保持不变时会起作用。
或者您使用 sapply
而不是 for-loop
,例如:
ref <- 1:5
names(ref) <- paste0("V", ref)
df <- cbind(df, sapply(ref, function(i) {rowMeans(df[,(i+1):6,drop=FALSE])/rowMeans(df[,(i+6):11,drop=FALSE])}))
df
# Rif A1 A2 A3 A4 A5 B1 B2 B3 B4 B5 V1 V2 V3 V4 V5
#1 a1 1 1 0 3 7 10 10 7 7 7 0.2926829 0.3548387 0.4761905 0.7142857 1.0000000
#2 b1 3 3 3 0 0 5 5 5 5 0 0.4500000 0.4000000 0.3000000 0.0000000 NaN
#3 c1 1 3 0 0 2 3 3 3 3 3 0.4000000 0.4166667 0.2222222 0.3333333 0.6666667
我有一个这样的数据框 (df):
Rif A1 A2 A3 A4 A5 B1 B2 B3 B4 B5
a1 1 1 0 3 7 10 10 7 7 7
b1 3 3 3 0 0 5 5 5 5 0
c1 1 3 0 0 2 3 3 3 3 3
我需要按如下方式计算 V 列表:
V1: mean(A5:A5)/mean(B5:B5)
V2: mean (A4:A5) / mean (B4:B5)
V3: mean (A3:A5) / mean (B3:B5)
....
我想我应该使用 for 循环。我试过下面写的循环,但它不起作用:
ref <- c(1, 2, 3, 4, 5)
dat1 <- NULL
for (i in 2:length(ref)) {
for (j in 1:nrow(df)) {
dat1[j] <- rowMeans(df[j,i-1:6])/rowMeans(df[j,i+6-1:11])
}
df[,paste0("V", i)] <- dat1
}
我期望的输出是这样的:
Rif A1 A2 A3 A4 A5 B1 B2 B3 B4 B5 V1 V2 V3 V4 V5
a1 1 1 0 3 7 10 10 7 7 7
b1 3 3 3 0 0 5 5 5 5 0
c1 1 3 0 0 2 3 3 3 3 3
继续你的步骤,你可以试试:
for (i in 1:5) {
df[,paste0("V", i)] <- rowMeans(df[,(i+1):6,drop=FALSE])/rowMeans(df[,(i+6):11,drop=FALSE])
}
df
# Rif A1 A2 A3 A4 A5 B1 B2 B3 B4 B5 V1 V2 V3 V4 V5
#1 a1 1 1 0 3 7 10 10 7 7 7 0.2926829 0.3548387 0.4761905 0.7142857 1.0000000
#2 b1 3 3 3 0 0 5 5 5 5 0 0.4500000 0.4000000 0.3000000 0.0000000 NaN
#3 c1 1 3 0 0 2 3 3 3 3 3 0.4000000 0.4166667 0.2222222 0.3333333 0.6666667
您可以跳过 j
- 循环,这是每行 sub-setting 然后使用 rowMeans
。另一方面,当您仅对一列进行子集化时,需要 drop=FALSE
。索引目前是硬编码的,但在数据结构保持不变时会起作用。
或者您使用 sapply
而不是 for-loop
,例如:
ref <- 1:5
names(ref) <- paste0("V", ref)
df <- cbind(df, sapply(ref, function(i) {rowMeans(df[,(i+1):6,drop=FALSE])/rowMeans(df[,(i+6):11,drop=FALSE])}))
df
# Rif A1 A2 A3 A4 A5 B1 B2 B3 B4 B5 V1 V2 V3 V4 V5
#1 a1 1 1 0 3 7 10 10 7 7 7 0.2926829 0.3548387 0.4761905 0.7142857 1.0000000
#2 b1 3 3 3 0 0 5 5 5 5 0 0.4500000 0.4000000 0.3000000 0.0000000 NaN
#3 c1 1 3 0 0 2 3 3 3 3 3 0.4000000 0.4166667 0.2222222 0.3333333 0.6666667