0

I have some data, that looks like the following:

"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

I have used the order function to order according to first "ID then "Start".

"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
…

Now I need to do two things: First I need to create a table that includes pairwise couples out of each ID group. For a group in one ID containing the names (1,2,3,4,5), I need to create the pairs (12,23,34,45). So for the above example, the pairs would be (Name_1+Name_3, Name_3+Name_7, Name_7+Name_5).

My output for the above example, would look like this:

"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,,
…

So I need to create pairs through ascending "Start", but within each "ID". I am thinking it should be done with a for loop, but I am a newbie, so pulling the data to a new table with the for loop confuses me in itself, and especially the constraint of doing it within each unique "ID", I have no idea how to do. I have experimented with splitting the data into groups according to ID using split, but it doesn't really get me further with creating the new data table.

I have created the ned data-table with the following code:

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)

And this is the table, I would like to write to. Is a for loop the write way to handle this, and if so, can you give me a few clues on how to start?

3
  • 2
    Please make your question easily reproducible. Commented Feb 12, 2015 at 15:28
  • Thank you for your comment. I could probably frame my question using mtcars. Would you recommend editing my question to this or just keep it in mind for next time? I ask, because I am afraid that changing my question now will cause further confusion. Commented Feb 16, 2015 at 9:28
  • Just keep it in mind next time. Commented Feb 16, 2015 at 13:07

1 Answer 1

1

It can be done with only one loop:

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

This will probably be horribly slow if the input is large. It can be optimized, but I didn't bother since using data.table is probably much quicker.

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 are different but otherwise the results are identical. The function used can probably be optimized as well.

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

identical(out, out2)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Your second solution is very elegant and works for me!
A problem has arisen for me, with the above second solution, and I cannot seem to find a way to fix it. I am trying to do the exact same as before, but on a new dataset, but when trying to run the out2-part, I get the following error: Error in data.table(..., key = key(..1)) : Item 1 has no length. Provide at least one item (such as NA, NA_integer_ etc) to be repeated to match the 2 rows in the longest column. Or, all columns can be 0 length, for insert()ing rows into.. I don't understand why, since the format of dt is the same. Can you decipher what the error means?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.