[R] convert a list to a data frame

David Winsemius dwinsemius at comcast.net
Wed Apr 4 23:47:56 CEST 2012


On Apr 4, 2012, at 4:54 PM, Sam Steingold wrote:

>> * Sam Steingold <fqf at tah.bet> [2012-04-04 14:58:15 -0400]:
>>
>> 1. the strings I replaced with ZZZZ and YYYY are very long; I wish I
>> could replace them in the object all, not just in text
>
> i.e., I have a long list with elements with long names.
> how do I replace all these long names with something shorter?
> row.names(list) <- ...
> does not do the trick.

Lists do not have rownames. They have names.

You _really_ should learn to post code that creates meaningful test  
cases. If, for example, you want to ask a question about a list to  
which you desire replacing names, then post:

testlis <- list(`ZZZZZZZZZZZ/2030` = "VECTOR/188",
                 `ZZZZZZZZZZZ/2031` = "VECTOR/187",
                 `ZZZZZZZZZZZ/2033` = "VECTOR/186",
                 `ZZZZZZZZZZZ/2037` = "VECTOR/185")

To which the respondent has only to copy that to a console to test the  
fairly simple solution :


 > names(testlis) <- sub("^Z+", "Z", names(testlis))
 > testlis
$`Z/2030`
[1] "VECTOR/188"

$`Z/2031`
[1] "VECTOR/187"

$`Z/2033`
[1] "VECTOR/186"

$`Z/2037`
[1] "VECTOR/185"

A quick way to construct possibly minimal examples from existing  
objects is to post the results of dput(head(listname))

-- 
David.
>
>>> lapply(all.v,names)[97:103]
>> $
>>
>> $`ZZZZZZZZZZZ/2031`
>> [1] "VECTOR/187"
>>
>> $`ZZZZZZZZZZZ/2032`
>> [1] "VECTOR/186"
>>
>> $`ZZZZZZZZZZZ/2033`
>> [1] "VECTOR/185"
>>
>> $`ZZZZZZZZZZZ/2034`
>> [1] "VECTOR/183"
>> [2] "VECTOR/184"
>>
>> $`ZZZZZZZZZZZ/2035`
>> [1] "VECTOR/182"
>>
>> $`ZZZZZZZZZZZ/2036`
>> [1] "VECTOR/181"
>>
>> for that I will need 2 lines in the resulting data frame.
>> how do I do that?
>
> done by a judicious use of rep.
>
> -- 
> Sam Steingold (http://sds.podval.org/) on Ubuntu 11.10 (oneiric) X  
> 11.0.11004000
> http://www.childpsy.net/ http://iris.org.il
> http://thereligionofpeace.com http://mideasttruth.com http://ffii.org
> Those who can laugh at themselves will never cease to be amused.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list