[R] add a list name into the list content in a new output

Chel Hee Lee chl948 at mail.usask.ca
Tue Jan 20 16:48:48 CET 2015


Hi Joshua,

You may use 'do.call()'.  Please see the output in below:

 > result <- lapply(names(op), function(x){
+   col1 <- x
+   col2 <- row.names(op[[x]])
+   mat <- op[[x]]
+   row.names(mat) <- NULL
+   rval <- cbind(col1, col2, mat)
+   names(rval) <- c("trait", "locus", names(mat))
+   rval
+ })
 > result
[[1]]
   trait    locus chr  pos ci.low ci.high  lod
1    bp c7.loc45   7 47.7  36.71    56.7 6.11
2    bp c15.loc8  15 12.0   3.96    22.8 5.29

[[2]]
   trait     locus chr  pos ci.low ci.high  lod
1    hr  c2.loc54   2 59.8   14.8    87.8 4.19
2    hr D15MIT184  15 22.8   12.0    36.0 3.15

[[3]]
   trait     locus chr pos ci.low ci.high  lod
1    bw c15.loc16  15  20     11      30 6.75

[[4]]
      trait     locus chr  pos ci.low ci.high  lod
1 heart_wt c12.loc49  12 51.2   28.2    62.2 3.65

 > do.call(rbind, result)
      trait     locus chr  pos ci.low ci.high  lod
1       bp  c7.loc45   7 47.7  36.71    56.7 6.11
2       bp  c15.loc8  15 12.0   3.96    22.8 5.29
3       hr  c2.loc54   2 59.8  14.80    87.8 4.19
4       hr D15MIT184  15 22.8  12.00    36.0 3.15
5       bw c15.loc16  15 20.0  11.00    30.0 6.75
6 heart_wt c12.loc49  12 51.2  28.20    62.2 3.65
 >

Is this what you are looking for?  I hope this helps.

Chel Hee Lee

On 1/20/2015 9:23 AM, Shuhua Zhan wrote:
> Hi Chel,
> Thank you very much for your help!
> I'm sorry I did not post my wanted output correctly. I only want the colnames of the data frame occur once as a table:
> trait locus  chr  pos ci.low ci.high  lod
>   bp c7.loc45   7 47.7  36.71    56.7 6.11
>   bp c15.loc8  15 12.0   3.96    22.8 5.29
>   hr c2.loc54    2 59.8   14.8    87.8 4.19
>   hr D15MIT184  15 22.8   12.0    36.0 3.15
>   bw c15.loc16  15  20     11      30 6.75
>   heart_wt c12.loc49  12 51.2   28.2    62.2 3.65
> How to add command to your script to scape those colnames?
> Thanks again,
> Joshua
>
> ----- Original Message -----
> From: "Chel Hee Lee" <chl948 at mail.usask.ca>
> To: "Shuhua Zhan" <szhan at uoguelph.ca>, "r-help" <r-help at r-project.org>
> Sent: Monday, January 19, 2015 10:44:23 PM
> Subject: Re: [R] add a list name into the list content in a new output
>
> I am just using the first two components of your output.
>
>   > op
> $bp
>            chr  pos ci.low ci.high  lod
> c7.loc45   7 47.7  36.71    56.7 6.11
> c15.loc8  15 12.0   3.96    22.8 5.29
>
> $hr
>             chr  pos ci.low ci.high  lod
> c2.loc54    2 59.8   14.8    87.8 4.19
> D15MIT184  15 22.8   12.0    36.0 3.15
>
>   >
>   > result <- lapply(names(op), function(x){
> +   col1 <- x
> +   col2 <- row.names(op[[x]])
> +   mat <- op[[x]]
> +   row.names(mat) <- NULL
> +   rval <- cbind(col1, col2, mat)
> +   names(rval) <- c("trait", "locus", names(mat))
> +   rval
> + })
>   >
>   > result
> [[1]]
>     trait    locus chr  pos ci.low ci.high  lod
> 1    bp c7.loc45   7 47.7  36.71    56.7 6.11
> 2    bp c15.loc8  15 12.0   3.96    22.8 5.29
>
> [[2]]
>     trait     locus chr  pos ci.low ci.high  lod
> 1    hr  c2.loc54   2 59.8   14.8    87.8 4.19
> 2    hr D15MIT184  15 22.8   12.0    36.0 3.15
>
>   >
>
> Is this what you are looking for?  I hope this helps.
>
> Chel Hee Lee
>
>
> On 01/19/2015 02:14 PM, Shuhua Zhan wrote:
>> Dear All,
>> I'd like to add a list name into the list contents to make a new output. The list is a list of data.frame derived from summary command in Rqtl. I want to add this list name to the data frame with a given column name such as "trait" and output this entire list as a table tab delimited as below.
>> Here is the list generated by summary command in Rqtl:
>>> summary(out.all, threshold=3, format="tabByCol")
>> bp:
>>            chr  pos ci.low ci.high  lod
>> c7.loc45   7 47.7  36.71    56.7 6.11
>> c15.loc8  15 12.0   3.96    22.8 5.29
>>
>> hr:
>>             chr  pos ci.low ci.high  lod
>> c2.loc54    2 59.8   14.8    87.8 4.19
>> D15MIT184  15 22.8   12.0    36.0 3.15
>>
>> bw:
>>             chr pos ci.low ci.high  lod
>> c15.loc16  15  20     11      30 6.75
>>
>> heart_wt:
>>             chr  pos ci.low ci.high  lod
>> c12.loc49  12 51.2   28.2    62.2 3.65
>>
>>
>> The new output I want:
>>
>> trait locus  chr  pos ci.low ci.high  lod
>> bp c7.loc45   7 47.7  36.71    56.7 6.11
>> bp c15.loc8  15 12.0   3.96    22.8 5.29
>>
>> trait locus  chr  pos ci.low ci.high  lod
>> hr c2.loc54    2 59.8   14.8    87.8 4.19
>> hr D15MIT184  15 22.8   12.0    36.0 3.15
>>
>> trait locus chr pos ci.low ci.high  lod
>> bw c15.loc16  15  20     11      30 6.75
>>
>> heart_wt locus chr  pos ci.low ci.high  lod
>> trait c12.loc49  12 51.2   28.2    62.2 3.65
>>
>> I appreciate for your help for any suggestions!!
>> Joshua
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>



More information about the R-help mailing list