如何使用循环从矩阵获取多个条件的多个子集集?

时间:2018-12-19 16:45:13

标签: r

我想问R的数据处理问题。

all_matrix<-structure(list(V1 = c(0.012, 0, 0, 0, 0.037, 0, 0, 0, 0.007, 0, 0.104, 0.149, 0.164, 0.258, 3.986, 0, 0.002, 0, 0, 0), V2 = c(0, 0.07, 0, 0, 0.017, 0, 0, 0, 0.025, 0, 2.322, 0.327, 0.134, 1.035, 2.732, 0.01, 1.097, 0.388, 0, 0), V3 = c(0, 0, 0, 0.005, 0, 0, 0, 0, 0, 0, 0.007, 0, 0, 0, 1.777, 0, 0.241, 0, 0, 0), V4 = c(0, 0, 0, 0.001, 0.003, 0, 0, 0, 0, 0, 0.207, 0.002, 0.003, 0.015, 0.032, 0, 0.007, 0, 0, 0), V5 = c(0, 0, 0, 0, 0.026, 0, 0, 0, 0.001, 0, 0.101, 0, 0, 0.005, 0.01, 0, 0, 0, 0, 0), V6 = c(0, 0, 0, 0.003, 0.009, 0, 0, 0, 0.076, 0, 0.01, 0.006, 0, 0.091, 0.829, 0, 0.002, 0, 0, 0), V7 = c(0, 0, 0, 0, 0.026, 0, 0, 0, 0.351, 0, 1.849, 0.003, 0, 0.005, 0.998, 0.009, 0.18, 0, 0, 0), V8 = c(0, 0, 0.002, 0.047, 0.01, 0.003, 0, 0, 0.021, 0, 0.848, 0.007, 0.005, 0.206, 0.023, 0, 0.025, 0, 0, 0), V9 = c(0, 0, 0, 0.02, 0.013, 0, 0, 0, 0, 0, 0.008, 0, 0, 0, 0, 0, 0, 0, 0, 0), V10 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.012, 9.895, 4.362, 0, 0, 0, 0, 0), P_diff = c(-4.051, -3.769, -3.602, -7.563, -6.398, -1.816, 0.84, -1.91, 3.095, -1.544, 6.068, 16.469, 6.403, 9.9, 9.089, 18.977, 14.123, 3.103, 1.527, -2.902), PH_fold = c(0.144, 0.511, 0.686, 0.372, 0.582, 0.325, 1.312, 0.436, 1.061, 0.371, 1.119, 1.298, 1.134, 1.146, 1.123, 1.484, 1.204, 1.263, 1.843, 0.423), PNH_fold = c(6.933, 1.955, 1.459, 2.69, 1.718, 3.081, 0.762, 2.291, 0.943, 2.696, 0.894, 0.77, 0.882, 0.872, 0.891, 0.674, 0.831, 0.792, 0.543, 2.366)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "P_diff", "PH_fold", "PNH_fold"), class = "data.frame", row.names = c("S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11", "S12", "S13", "S14", "S15", "S16", "S17", "S18", "S19", "S20"))
sig1<-data.frame(subset(all_matrix, all_matrix$PH_fold >= 1.1 & all_matrix$P_diff >=1))##conditions are in range like all_matrix$PH_fold >= 1.1,1.2,1.3,....,2 & all_matrix$P_diff >= 1,2,3,4......, upto 20###   
sig2<-data.frame(subset(all_matrix, all_matrix$PNH_fold >= 1.1 & all_matrix$P_diff <= -1))##conditions are in range like all_matrix$PNH_fold >= 1.1,1.2,1.3,....,2 & all_matrix$P_diff >= -1,-2,-3,-4......, upto -20###
d <- function(x){sum((log(x[x>0]))*(x[x>0]))*(-1)}
sh1<-apply((sig1[,-c(5:7)]/100), 2, d)
sh2<-apply((sig2[,-c(5:7)]/100), 2, d)
count1<-apply(sig1[,-c(5:7)], 2, function(i) (sum(i > 0)))
count2<-apply(sig2[,-c(5:7)], 2, function(i) (sum(i > 0)))
G1<-(sig1*sh1)
G2<-(sig2*sh2)
G<-data.frame(G1/G2)

我想设置一个R代码,根据上述每个条件为all_matrix的所有子集计算“ G”。

那么我如何使用循环为每个子集使用不同的条件获得矩阵的子集,以进行进一步的过程来计算“ G”:

我想循环使用subset()函数:

有人可以帮我吗?

谢谢!

我感谢您的答复!

1 个答案:

答案 0 :(得分:0)

如果数据框较大,请不要使用subset,它比其他任何选项都要慢。通常最快的是索引方式。

我首先在给定条件下创建两个索引。然后是保存子DFS的列表。

i1 <- data1$age < 15
i2 <- data1$age > 20

sub_data1 <- list()
sub_data1[['less_15']] <- data1[i1, ]
sub_data1[['over_20']] <- data1[i2, ]

请注意,您可以跳过索引创建代码,只需执行

sub_data1 <- list()
sub_data1[['less_15']] <- data1[data1$age < 15, ]
sub_data1[['over_20']] <- data1[data1$age > 20, ]