[R] help with combining matrix and list into dataframe

Duncan Murdoch murdoch at stats.uwo.ca
Thu Jan 3 21:12:14 CET 2008


On 1/3/2008 2:52 PM, Manisha Brahmachary wrote:
> Problem#################################################################
> 
> I am having a problem combining a matrix and elements of a list into one data
> frame.
> 
> Data####################################################################
> 
> The matrix is ludwig.results and dim of matrix is 213,8.
> 
> The list is called symbols and its length is 213.  

Use unlist to convert the list to an atomic vector.  This will lose the 
class information on each element; hopefully you don't need that.

Then you can use cbind to bind it to a dataframe, no for loop needed.

Duncan Murdoch

> 
> Following is an example the structure of my matrix (ludwig.results): 
> 
>  
> 
>      Row    Gene ID      Gene Name Score(d)           Numerator(r)
> Denominator(s+s0)    Fold Change        q-value(%)        
> 
> [1,] "9507" "1427276_at" "9506"    "4.60382496146004" "0.418481878487541"
> "0.0908987378952882" "1.33515425467743" "4.80974775545105"
> 
> [2,] "8927" "1426653_at" "8926"    "4.45317231159082" "0.319591808000491"
> "0.0717672224738867" "1.24728944266546" "4.80974775545105"
> 
>  
> 
> The list symbols has been generated from the aanaffy package. Following is an
> example of the list (symbols) :
> 
>  
> 
> [[1]]
> 
> [1] "Smc4"
> 
> attr(,"class")
> 
> [1] "aafSymbol"
> 
>  
> 
> [[2]]
> 
> [1] "Mcm3"
> 
> attr(,"class")
> 
> [1] "aafSymbol"
> 
>  
> 
> attr(,"class")
> 
> [1] "aafList"
> 
>  
> 
> I want to combine the data in the matrix with the corresponding gene symbols
> in the list symbols. The order in which the gene symbols are stored in
> symbols
> 
> is the same as the order in the matrix.
> 
>  
> 
> Currently the code I have written is as follows:
> 
>  
> 
> for (i in 1:length(symbols)){
> 
>      temp.results[i]<- cbind(ludwig.results[i],symbols[[i]])
> 
>     new.results<-rbind(new.results,temp.results[i])
> 
>            }
> 
> I get the following error using this code:
> 
>  
> 
> Error in `[<-.data.frame`(`*tmp*`, i, value = c("9507", "Smc4")) : 
> 
>         replacement has 1 rows, data has 0
> 
>  
> 
> Can someone suggest me how to correct this code and get the desired results.
> 
>  
> 
> This is the format I want it to be.
> 
>  
> 
> Row              Gene ID            Gene Name             Score(d)
> Numerator(r)    Denominator(s+s0)          Fold Change           q-value(%)
> symbol
> 
>   "9507"         "1427276_at"               "9506"   "4.60382496146004"
> "0.418481878487541" "0.0908987378952882"   "1.33515425467743"
> "4.80974775545105"  "Smc4"
> 
>   "8927"         "1426653_at"               "8926"   "4.45317231159082"
> "0.319591808000491" "0.0717672224738867"   "1.24728944266546"
> "4.80974775545105"  "Mcm3"
> 
>  
> 
>  
> 
>  
> 
> Thanks in advance.
> 
>  
> 
> Manisha
> 
>  
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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