Hi Philipp, thanks for your answer. I tried merge but got an error. I think I have a solution using a for loop, but I'm pretty sure there's a better way to do it using Map, except that my vectors are regular numeric vectors not S4 GR objects. I'll keep hacking at it, but if you have another idea, I'll appreciate it. Thanks!
Dear BioStars,
I have a genomic ranges (GR) list object called lgr.test and would like to simply add several columns to its metadata. How would I do that? I have a list of numeric vectors lvec.test of the same lengths as the GRs in lgr.test.
Thank you!
> summary(lvec.test)
Length Class Mode
chr1 589 -none- numeric
chr2 790 -none- numeric
chr3 482 -none- numeric
chr4 681 -none- numeric
chr5 698 -none- numeric
chr6 492 -none- numeric
chr7 713 -none- numeric
chr8 489 -none- numeric
chr9 590 -none- numeric
chr10 521 -none- numeric
chr11 853 -none- numeric
chr12 395 -none- numeric
chr13 373 -none- numeric
chr14 358 -none- numeric
chr15 388 -none- numeric
chr16 340 -none- numeric
chr17 465 -none- numeric
chr18 287 -none- numeric
chr19 357 -none- numeric
> summary(lgr.test)
Length Class Mode
chr1 589 GRanges S4
chr2 790 GRanges S4
chr3 482 GRanges S4
chr4 681 GRanges S4
chr5 698 GRanges S4
chr6 492 GRanges S4
chr7 713 GRanges S4
chr8 489 GRanges S4
chr9 590 GRanges S4
chr10 521 GRanges S4
chr11 853 GRanges S4
chr12 395 GRanges S4
chr13 373 GRanges S4
chr14 358 GRanges S4
chr15 388 GRanges S4
chr16 340 GRanges S4
chr17 465 GRanges S4
chr18 287 GRanges S4
chr19 357 GRanges S4
2 answers
Ok, a for loop works (see below), but if some GR gurus see this, please save me from my ignorance and tell me how to do this using standard GR functions (Map , endoapply, mendoapply, Reduce). The for solution is fast enough but doesn't use the names of the objects in the lists, so it depends entirely on their order.
Cheers!
For solution:
for (i in 1:length(lgr.test)) { values(lgr.test[[i]])$NEW<-lvec.test[[i]] }
One lazy way is to use the merge function:
merged <- merged(lvec.test, lgr.test, by='row.names')
This won't give you the final product, it will give you something like:
Row.names Length.x Class.x Mode.x Length.y Class.y Mode.y
chr1 589 NA numeric 589 GRanges S4 ...
You can then clean it up and drop the columns you don't like, for example by
rownames(merged) <- merged[,1]
merged <- merged[-1]
merged <- merged[-2] etc.
No worries! What's the error exactly?
You may be able to circumvent it by putting as.data.frame() around lvec.test and lgr.test
Hi Philipp, I tried both (with and without as.data.frame but get an error referring to a difference in row numbers):
arguments imply differing number of rows: 589, 790, 482, 681, 698, 492, 713, 489, 590, 521, 853, 395, 373, 358, 388, 340, 465, 287, 357
Aaah your two tables have different row-lengths - you can try adding all.x = TRUE or all.y = TRUE to the merge command:
If all.x is true, all the non matching cases of x are appended to the result as well, with NA filled in the corresponding columns of y; analogously for all.y.
Log in to answer this question.