R:For循环嵌套在for循环中

时间:2015-02-12 15:20:27

标签: r for-loop

我有一些数据,如下所示:

"Name","Length","Startpos","Endpos","ID","Start","End","Rev","Match"    
"Name_1",140,0,138,"1729",11,112,0,1
"Name_2",132,0,103,"16383",23,232,0,1
"Name_3",102,0,100,"1729",22,226,1,1
"Name_4",112,0,130,"16383",99,992,1,1
"Name_5",132,0,79,"1729",81,820,1,1
"Name_6",112,0,163,"16383",81,820,0,1
"Name_7",123,0,164,"1729",54,542,1,1
"Name_8",123,0,65,"16383",28,289,0,1

我已使用order功能根据第一个“ID然后”开始“。

进行排序
"Name","Length","Startpos","Endpos","ID","Start","End","Rev","Match"   
"Name_1",140,0,138,"1729",11,112,0,1
"Name_3",102,0,100,"1729",22,226,1,1
"Name_7",123,0,164,"1729",54,542,1,1
"Name_5",132,0,79,"1729",81,820,1,1
"Name_2",132,0,103,"16383",23,232,0,1
"Name_8",123,0,65,"16383",28,289,0,1
…

现在我需要做两件事: 首先,我需要创建一个包含每个ID组的成对耦合的表。对于包含名称(1,2,3,4,5)的一个ID中的组,我需要创建对(12,23,34,45)。因此,对于上面的示例,对将是(Name_1 + Name_3,Name_3 + Name_7,Name_7 + Name_5)。

上面示例的输出如下所示:

"Start_Name_X","Start_Name_Y","Length_Name_X","Length_Name_Y","Name_Name_X","Name_Name_Y","ID","New column"
11, 22, 140, 102, "Name_1", Name_3", 1729,,
22, 54, 102, 123, "Name_3", Name_7, 1729,,
54, 81, 123, 132, "Name_7", Name_5, 1729,,
23, 28, 132, 123, "Name_2", "Name_8", 16383,,
…

所以我需要通过升序“开始”来创建对,但是在每个“ID”内。 我认为它应该用一个for循环来完成,但我是一个新手,所以将数据拉到一个带有for循环的新表中会让我感到困惑,尤其是在每个独特的环境中这样做的约束“ ID“,我不知道该怎么做。 我已尝试使用split根据ID将数据拆分为组,但它并没有让我更进一步创建新的数据表。

我使用以下代码创建了ned数据表:

column_names = data.frame(Start_Name_X ="Start_Name_x",
Start_Name_Y="Start_Name_Y", Length_Name_X ="Length_Name_X",
Length_Name_Y="Length_Name_Y", Name_X="Name_X", Name_Y="Name_Y", ID="ID",
New_Column="New_Column")

write.table(column_names, file = "datatabel.csv", row.names=FALSE, append =
FALSE, col.names = FALSE, sep=",", quote=TRUE)

这是表,我想写信给。 for循环是一种写入方式来处理这个问题,如果有的话,你能给我一些关于如何开始的线索吗?

1 个答案:

答案 0 :(得分:1)

只需一个循环就可以完成:

df <- read.table(sep = ",", header = TRUE, stringsAsFactors = FALSE,
text = "\"Name\",\"Length\",\"Startpos\",\"Endpos\",\"ID\",\"Start\",\"End\",\"Rev\",\"Match\"\n\"Name_1\",140,0,138,\"1729\",11,112,0,1\n\"Name_2\",132,0,103,\"16383\",23,232,0,1\n\"Name_3\",102,0,100,\"1729\",22,226,1,1\n\"Name_4\",112,0,130,\"16383\",99,992,1,1\n\"Name_5\",132,0,79,\"1729\",81,820,1,1\n\"Name_6\",112,0,163,\"16383\",81,820,0,1\n\"Name_7\",123,0,164,\"1729\",54,542,1,1\n\"Name_8\",123,0,65,\"16383\",28,289,0,1",
    )

df <- df[order(df$ID, df$Start), ]

inds <- c("Name", "Start", "Length")
indsSorted <- c("Start_Name_X","Start_Name_Y","Length_Name_X","Length_Name_Y","Name_Name_X","Name_Name_Y","ID","New_Column")

out <- data.frame(matrix(nrow = 0, ncol = 8))
colnames(out) <- c("Start_Name_X","Start_Name_Y","Length_Name_X","Length_Name_Y","Name_Name_X","Name_Name_Y","ID","New_Column")
for (i in unique(df$ID)){
    dfID <- subset(df, ID == i)
    dfHead <- head(dfID, n = nrow(dfID) - 1)[, inds]
    colnames(dfHead) <- paste0(colnames(dfHead), "_Name_X")

    dfTail <- tail(dfID, n = nrow(dfID) - 1)[, inds]
    colnames(dfTail) <- paste0(colnames(dfTail), "_Name_Y")

    out <- rbind(out, cbind(dfHead, dfTail, ID = i, New_Column = '', stringsAsFactors = FALSE)[, indsSorted])
}
  out

如果输入很大,这可能会非常慢。它可以进行优化,但我没有打扰,因为使用data.table可能要快得多。

dt <- data.table(df, key = "ID,Start")
fn <- function(dtIn, id){
    dtHead <- head(dtIn, n = nrow(dtIn) - 1)
    setnames(dtHead, paste0(colnames(dtHead), "_Name_X"))

    dtTail <- tail(dtIn, n = nrow(dtIn) - 1)
    setnames(dtTail,  paste0(colnames(dtTail), "_Name_Y"))

    cbind(dtHead, dtTail, ID = id, New_Column = '')
}

out2 <- dt[, fn(.SD, ID), by = ID, .SDcols = c("Name", "Start", "Length")]
out2 <- as.data.frame(out2[, indsSorted, with = FALSE])

Rownames是不同的,但结果是相同的。使用的功能也可以进行优化。

rownames(out) <- NULL
rownames(out2) <- NULL

identical(out, out2)