Skip to content
Back to formatted view

Raw Message

Message-ID: <1403663243.83650.YahooMailNeo@web142602.mail.bf1.yahoo.com>
Date: 2014-06-25T02:27:23Z
From: arun
Subject: converting a list with named member to a vector maintaining original names
In-Reply-To: <1403662800.58699.YahooMailNeo@web142606.mail.bf1.yahoo.com>

To get the vector
setNames(stack(test)[,1],stack(test)[,2])
#ABCC2 ABCC5 ABCC5 
#37280 12268 13308 

#or
setNames(unlist(test),stack(test)[,2])
#ABCC2 ABCC5 ABCC5 
#37280 12268 13308 
A.K.





On , arun <smartpink111 at yahoo.com> wrote:


You could do:
stack(test)[,2:1]
#??? ind values
#1 ABCC2? 37280
#2 ABCC5? 12268
#3 ABCC5? 13308

A.K.




On Tuesday, June 24, 2014 2:09 PM, C Lin <baccts at hotmail.com> wrote:
Dear R users,

I have a list that I'd like to convert to a vector while preserving the original names.
For example:

test <- list(ABCC2=37280,ABCC5=c(12268,13308));

If I do unlist, it automatically renamed the ABCC5 to ABCC51 and ABCC52
> unlist(test)
?ABCC2 ABCC51 ABCC52?
?37280 ?12268 ?13308?

How do I retained the original names so I'll get:
ABCC2 37280
ABCC5 12268
ABCC5 13308

Thank you in advance for your help.

Lin? ??? ???? ??? ?? ??? ??? ? 
______________________________________________
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.