Arrays 如何对多行数据执行一个函数,这些数据是由R中的列分解的?
我在一个文件中有一个表,其中有许多行,我使用Arrays 如何对多行数据执行一个函数,这些数据是由R中的列分解的?,arrays,r,function,data.table,Arrays,R,Function,Data.table,我在一个文件中有一个表,其中有许多行,我使用 data <-read.table("path/to/data.txt",header=TRUE, sep="\t",row.names=1) A1 A2 A3 B1 B2 B3 Row1 1 3 2 3 2 6 Row2 3 2 1 3 6 7 ... 但是我不确定
data <-read.table("path/to/data.txt",header=TRUE, sep="\t",row.names=1)
A1 A2 A3 B1 B2 B3
Row1 1 3 2 3 2 6
Row2 3 2 1 3 6 7
...
但是我不确定如何从数据框中为每一行指定A和B,是否有一种方法可以同时为整个数据表指定这一切?我必须将数据读入一个帧,还是可以直接从最初的read.table数据开始工作 试试这个:
set.seed(001) # Generating some data
DF <- data.frame(A1=sample(1:9, 10, T),
A2=sample(1:9, 10, T),
A3=sample(1:9, 10, T),
B1=sample(1:9, 10, T),
B2=sample(1:9, 10, T),
B3=sample(1:9, 10, T))
sampA <- DF[,grep('A', names(DF))] # Sample with columns A
sampB <- DF[,grep('B', names(DF))] # Sample with columns B
lapply(1:nrow(DF), function(i){
wilcox.test(as.numeric(sampA[i,]), as.numeric(sampB[i,]), exact=FALSE )
}) # Performing the test
我只显示了前3个结果,完整的列表长度是10,因为
DF
有10行。到目前为止您尝试了什么?“比较A样本和B样本”是什么意思?您期望的结果是什么?具体来说,我想在每一行上执行wilcoxon.test函数,但我不确定如何指定条目,语法是wilcox.test(sampA,sampB)
,我想在表格的每一行上执行。对于第1行,sampA将是第一行(1,3,2)中A1、A2、A3下的三个条目sampB将是第一行(3,2,6)中B1、B2、B3下的三个条目,但我希望对文件中的每一行都这样做。我是R的新手,我到处都找过了,但我无法找到如何指定特定的列和行,我知道如何调用我可以使用的第一行data[1,]
但我找不到只调用前3列或最后3列的语法,例如,我还尝试使用factor、factor(c(1,1,1,2,2))
但我一直在思考如何指定1和2的hanks@Jilber-在您的示例中,每个htest输出似乎将自己保存到一个不同的条目,即[[1]]、[[2]]和[[3]]。我想将输出保存到一个文本文件中,但不知道该怎么做,因为我不能使用write.table来实现这一点。是否可以从htest结果中将W和p值变灰,并将其粘贴到可以保存到文本文件的框架中?您好-我刚刚计算出来并使用了data.frame(unlist(output))
。再次感谢@用户1637359,很高兴有用。你也可以投票决定答案。
function(A,B)
set.seed(001) # Generating some data
DF <- data.frame(A1=sample(1:9, 10, T),
A2=sample(1:9, 10, T),
A3=sample(1:9, 10, T),
B1=sample(1:9, 10, T),
B2=sample(1:9, 10, T),
B3=sample(1:9, 10, T))
sampA <- DF[,grep('A', names(DF))] # Sample with columns A
sampB <- DF[,grep('B', names(DF))] # Sample with columns B
lapply(1:nrow(DF), function(i){
wilcox.test(as.numeric(sampA[i,]), as.numeric(sampB[i,]), exact=FALSE )
}) # Performing the test
[[1]]
Wilcoxon rank sum test with continuity correction
data: as.numeric(sampA[i, ]) and as.numeric(sampB[i, ])
W = 3, p-value = 0.6579
alternative hypothesis: true location shift is not equal to 0
[[2]]
Wilcoxon rank sum test with continuity correction
data: as.numeric(sampA[i, ]) and as.numeric(sampB[i, ])
W = 0, p-value = 0.0722
alternative hypothesis: true location shift is not equal to 0
[[3]]
Wilcoxon rank sum test with continuity correction
data: as.numeric(sampA[i, ]) and as.numeric(sampB[i, ])
W = 6, p-value = 0.6579
alternative hypothesis: true location shift is not equal to 0