Convertin rows of a matrix to a list

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Convertin rows of a matrix to a list

Aleš Žiberna-3
Hello!

I would like to convert rows of a matrix to a elements of a list.

#For example, if I have
mat<-matrix(1:100,ncol=5, nrow=20)

#I can do:
list<-apply(mat,1,list)
list
#however this is not quite what I want. To get what I want, I have to do:
list<-lapply(list,function(x)x[[1]])
list

Is there a faster way?

Best regards,
Ales Ziberna

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Convertin rows of a matrix to a list

Liaw, Andy
Perhaps something like:

    as.list(as.data.frame(t(mat)))

Andy

From: Aleš Žiberna

>
> Hello!
>
> I would like to convert rows of a matrix to a elements of a list.
>
> #For example, if I have
> mat<-matrix(1:100,ncol=5, nrow=20)
>
> #I can do:
> list<-apply(mat,1,list)
> list
> #however this is not quite what I want. To get what I want, I
> have to do:
> list<-lapply(list,function(x)x[[1]])
> list
>
> Is there a faster way?
>
> Best regards,
> Ales Ziberna
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Convertin rows of a matrix to a list

Aleš Žiberna-3
In reply to this post by Aleš Žiberna-3
Thanks to all,
especially to Patrick Burns, who's solution is the fastest!

Best regards,
Ales

Patrick Burns pravi:

> Untried, but does this work for you:
>
> split(mat, row(mat))
>
> Patrick Burns
> [hidden email]
> +44 (0)20 8525 0696
> http://www.burns-stat.com
> (home of S Poetry and "A Guide for the Unwilling S User")
>
> Aleš Žiberna wrote:
>
>> Hello!
>>
>> I would like to convert rows of a matrix to a elements of a list.
>>
>> #For example, if I have
>> mat<-matrix(1:100,ncol=5, nrow=20)
>>
>> #I can do:
>> list<-apply(mat,1,list)
>> list
>> #however this is not quite what I want. To get what I want, I have to
>> do:
>> list<-lapply(list,function(x)x[[1]])
>> list
>>
>> Is there a faster way?
>>
>> Best regards,
>> Ales Ziberna
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>>
>>
>>
>>  
>>
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html