[R] Creating named.list from two matrix columns

jim holtman jholtman at gmail.com
Tue Sep 7 00:25:04 CEST 2010


The quotes are not unnecessary since 'row:1' is not a valid name and
therefore you will need quotes to refer to the data:

> x<-cbind(c("row:1", "row:2", "row:3"), c("4889", "9987", "494"))
> x1<-as.list(x[,2])
> names(x1)<-x[,1]
> x1
$`row:1`
[1] "4889"

$`row:2`
[1] "9987"

$`row:3`
[1] "494"

> str(x1)
List of 3
 $ row:1: chr "4889"
 $ row:2: chr "9987"
 $ row:3: chr "494"
> x1$row:1  # causes an error
Error in x1$row:1 : argument of length 0
No suitable frames for recover()
> x1$'row:1'  # now you can access the value
[1] "4889"
>
> x1[['row:1']]  # another way of accessing it with a character string
[1] "4889"



On Mon, Sep 6, 2010 at 12:29 PM, Viki S <isvik at live.com> wrote:
> Hi Jim,
> Thanks,
>
> That´s right. But the problem is that it introduces unnecessary quotes,
> perhaps due to the format of first column data in this case :
>
> x<-cbind(c("row:1", "row:2", "row:3"), c("4889", "9987", "494"))
> x1<-as.list(x[,2])
> names(x1)<-x[,1]
>> x1
> $`row:1`
> [1] "4889"
>
> $`row:2`
> [1] "9987"
>
> $`row:3`
> [1] "494"
>
> How can I avoid unnecessary ` `quotes around the names ?
>
> V
>
>> Date: Mon, 6 Sep 2010 09:14:23 -0400
>> Subject: Re: [R] Creating named.list from two matrix columns
>> From: jholtman at gmail.com
>> To: isvik at live.com
>> CC: r-help at r-project.org
>>
>> Is this what you want:
>>
>> > x
>> V1 V2
>> 1 row1 2334
>> 2 row2 347
>> 3 row3 379
>> > x.list <- as.list(x$V2)
>> > names(x.list) <- x$V1
>> > x.list
>> $row1
>> [1] 2334
>>
>> $row2
>> [1] 347
>>
>> $row3
>> [1] 379
>>
>>
>>
>> On Mon, Sep 6, 2010 at 7:55 AM, Viki S <isvik at live.com> wrote:
>> >
>> > Hi Friends,
>> > I am new to R.
>> >
>> > On R utility class pages, creating "named.list" is described with this
>> > command :
>> > new("named.list",a=1,b=2)
>> >
>> >
>> > For large matrix having two columns, such as :
>> >
>> > "row1"   2334
>> > "row2"   347
>> > "row3"   379
>> > ...
>> >
>> > I want to create a named.list like :
>> > $row1
>> > [1] 2334
>> >
>> > $row2
>> > [1] 347
>> >
>> > ...
>> >
>> > Can anyone explain how "named.list" variable can be created by using two
>> > specified columns of a dataframe or matrix object, where one of the two
>> > columns is assigned as a name (string) and
>> > other as its corresponding value ?
>> >
>> > Thanks
>> >
>> >        [[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.
>> >
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list