我想在R中执行至少六个循环步骤。我的数据集28 files存储在一个文件夹中。每个文件有22行(21个单独的案例和一列用于列名称)和列,如下所示:Id,id,PC1,PC2 ... .PC20。
我打算:
按如下方式排列每个数据框:
第一栏应为“id”和
接下来的十列应该是前十台PC(PC1,PC2,...... PC10)
目前我能够为每对数据手动完成(代码如下):
## 1. step
## read files into R as a data frame
c_2d_hand_1a<-read.table("https://googledrive.com/host/0B90n5RdIvP6qbkNaUG1rTXN5OFE/PC scores, c_2d_hand-1a, Symmetric component.txt",header=T)
c_2d_hand_1b<-read.table("https://googledrive.com/host/0B90n5RdIvP6qbkNaUG1rTXN5OFE/PC scores, c_2d_hand-1b, Symmetric component.txt",header=T)
## 2. step
## delete first column named “Id” in the each data frame
c_2d_hand_1a[,1]<-NULL
c_2d_hand_1b[,1]<-NULL
## 3. step
## arrange each data frame that have 21 rows and 11 columns (id,PC1,PC2..PC10)
c_2d_hand_1a<-c_2d_hand_1a[,1:11]
c_2d_hand_1b<-c_2d_hand_1b[,1:11]
## 4. step
## sort each data frame according to “id”
c_2d_hand_1a<-c_2d_hand_1a[order(c_2d_hand_1a$id),]
c_2d_hand_1b<-c_2d_hand_1b[order(c_2d_hand_1b$id),]
## 5. step
## perform pairwise comparison by protest function
library(permute)
library(vegan)
c_2d_hand_1a_c_2d_hand_1b<-protest(c_2d_hand_1a[,2:ncol(c_2d_hand_1a)],c_2d_hand_1b[,2:ncol(c_2d_hand_1b)],permutations=10000)
summary(c_2d_hand_1a_c_2d_hand_1b)[2] ## or c_2d_hand_1a_c_2d_hand_1b[3]
由于我是R中数据处理/操作的新手,我的自学习技巧适合手动执行各个步骤,为每个数据集键入代码并在当时执行每个成对比较。由于我需要执行这六个步骤378次,因此手动输入将是详尽且耗时的。
我尝试将文件作为列表导入并尝试了几个操作,但我没有成功。具体来说,使用list.files(),我创建了名为“probe”的列表。我能够使用例如选择某些数据帧。探针[2]。我也可以通过例如评估列“Id”来评估探针[2] [1],并通过探针[2] [1]&lt; -NULL删除它。但是当我尝试使用for循环时,我被卡住了。
答案 0 :(得分:0)
此代码未经测试,但幸运的是,它应该可以正常运行。抗议()结果的摘要存储在列表矩阵中。
# develop a way to easily reference all of the URLs
url.begin <- "https://googledrive.com/host/0B90n5RdIvP6qbkNaUG1rTXN5OFE/PC scores, "
url.middle <- c("c_2d_hand-1a", "c_2d_hand-1b")
url.end <- ", Symmetric component.txt"
L <- length(url.middle)
# read in all of the data and save it to a list of data frames
mybiglist <- lapply(url.middle, function(mid) read.table(paste0(url.begin, mid, url.end), header=TRUE))
# save columns 2 to 12 in each data frame and order by id
mybiglist11cols <- lapply(mybiglist, function(df) df[order(df$id), 2:12])
# get needed packages
library(permute)
library(vegan)
# create empty matrix of lists to store results
results <- matrix(vector("list", L*L), nrow=L, ncol=L)
# perform pairwise comparison by protest function
for(i in 1:L) {
for(j in 1:L) {
df1 <- mybiglist11cols[[i]]
df2 <- mybiglist11cols[[j]]
results[i, j] <- list(summary(protest(df1[, -1], df2[, -1], permutations=10000)))
}}