1

我在一个文件中有一个表,其中有很多行,我已经读入 R 使用

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
    ...

然后我使用

df <-data.frame(data)

我想执行一个函数()来比较每行的 A 样本和 B 样本,

function(A,B)

但我不确定如何只为每一行指定数据框中的 A 和 B - 有没有办法为整个数据表一次完成所有这些操作?我是否必须将数据读入帧中,还是可以直接从初始的 read.table 数据开始工作?

4

1 回答 1

2

试试这个:

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 

我只显示了前 3 个结果,完整的列表长度为 10,因为DF有 10 行。

于 2012-10-16T13:46:39.710 回答