Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为r中的大型数据帧的nC2迭代列编写循环?_R - Fatal编程技术网

为r中的大型数据帧的nC2迭代列编写循环?

为r中的大型数据帧的nC2迭代列编写循环?,r,R,上面是我的dataframe格式,我想以这样一种方式编写一个循环:对于每个迭代,我需要数据子集,如 c1 c2 60 7.0 65.9.0 57.6.0 68 20.0 23 0 我的意思是我想访问列c1和c2、c1和c3、c1和c4、c1和c5、c2和c3、c2和c4、c2和c5、c3和c4、c3和c5、c4和c5 如果我们需要列表中的输出(假设数据集是data.table),我不想为此编写一个循环。,使用列名称模式“c”后跟数字对列进行子集,然后使用combn Patient Date

上面是我的dataframe格式,我想以这样一种方式编写一个循环:对于每个迭代,我需要数据子集,如

c1 c2
60 7.0
65.9.0
57.6.0
68 20.0
23 0
我的意思是我想访问列c1和c2、c1和c3、c1和c4、c1和c5、c2和c3、c2和c4、c2和c5、c3和c4、c3和c5、c4和c5


如果我们需要
列表中的输出(假设数据集是
data.table
),我不想为此编写一个循环。

,使用列名称模式“c”后跟数字对列进行子集,然后使用
combn

  Patient  Date      c1   c2   c3   c4   c5      c6       c7
1: xyz    01-AUG-14  60  7.0   12  9.0  0.00     34       6.700
2: pqr    05-SEP-14  65  9.0   34 11.0  0.76     12       5.180
3: asd    08-AUG-14  57  6.0   45 12.0  0.00     12       4.830
4: we     10-JUL-14  68 20.0   78 13.0  0.00     45       3.560
5: zxc    14-OCT-14  23   0   11  34.6  0.00     67          

nm1假设您希望所有列的组合
c1、c2等
传递给函数/循环,同时您希望访问
患者
日期

为了演示逻辑,创建了一个简单的
print
函数来组合每个组

nm1 <- grep("c\\d+", names(d1), value = TRUE)
lst <- combn(d1[, ..nm1], 2, FUN = list)
lst[[1]]
#   c1 c2
#1: 60  7
#2: 65  9
#3: 57  6
#4: 68 20
#5: 23  0

你为什么不学习57 6.0?是的,你必须只学习57 6.0。我刚刚演示了如何获取矩阵中的子元素的格式(r,nrow=len.r,ncol=count):“data”必须是向量类型,是“NULL”吗@Asha在哪一步出现了错误?你说你得到了输出和错误?你得到了我回答中提到的输出吗?@Asha我修改了我的回答,添加了
simplify=FALSE
。该函数的最终返回值可能是导致您端出现问题的原因。@Asha Cool。如果你对答案满意,你可以。
# Function to receive each group of combinations along with Patient & Date
myprint <- function(x,Patient,Date){
  print(cbind.data.frame(Patient,Date,x))
}

The data:
df1 <- read.table(text = "Patient  Date  c1 c2 c3 c4  c5  c6  c7
1: xyz    01-AUG-14  60  7.0   12  9.0  0.00     34       6.700
2: pqr    05-SEP-14  65  9.0   34 11.0  0.76     12       5.180
3: asd    08-AUG-14  57  6.0   45 12.0  0.00     12       4.830
4: we     10-JUL-14  68 20.0   78 13.0  0.00     45       3.560
5: zxc    14-OCT-14  23   0   11  34.6  0.00     67       3.0", 
header = TRUE, 
stringsAsFactors = FALSE)
>combn(df1[,3:ncol(df1)], 2, myprint, simplify = FALSE, Patient = df1[,1], Date = df1[,2])
#   Patient      Date c1 c2
#1:     xyz 01-AUG-14 60  7
#2:     pqr 05-SEP-14 65  9
#3:     asd 08-AUG-14 57  6
#4:      we 10-JUL-14 68 20
#5:     zxc 14-OCT-14 23  0
#   Patient      Date c1 c3
#1:     xyz 01-AUG-14 60 12
#2:     pqr 05-SEP-14 65 34
#3:     asd 08-AUG-14 57 45
#4:      we 10-JUL-14 68 78
#5:     zxc 14-OCT-14 23 11
#   Patient      Date c1   c4
#1:     xyz 01-AUG-14 60  9.0
#2:     pqr 05-SEP-14 65 11.0
#3:     asd 08-AUG-14 57 12.0
#4:      we 10-JUL-14 68 13.0
#5:     zxc 14-OCT-14 23 34.6
...............
................