aiguo li
2020-Apr-09 15:00 UTC
[R] create a r list from dataframe using the first column as list names
Hello allI need to create a r list with each row as a list object and named with the element in the first column.? Illustrated below:> a<- as.data.frame(matrix(LETTERS[1:16],nrow = 4))> a? V1 V2 V3 V41 ?A ?E ?I ?M2 ?B ?F ?J ?N3 ?C ?G ?K ?O4 ?D ?H ?L ?P I want the list looks like$A[1] E I MLevels: E I M $B[1] F J NLevels: F J N $C[1] G K OLevels: G K O $D[1] H L PLevels: H L P I used the script below, it does not work the way I wantlapply(split(a,a$V1), function(x) as.list(a[-1]))? any help would be greatly appreciated! Anna [[alternative HTML version deleted]]
Rasmus Liland
2020-Apr-09 17:28 UTC
[R] create a r list from dataframe using the first column as list names
On 2020-04-09 15:00 +0000, aiguo li via R-help wrote: | Hello allI need to create a r list with | each row as a list object and named with | the element in the first column.? Dear aiguo, Perhaps this fits your bill? a <- matrix(LETTERS[1:16], nrow = 4) FUN <- function(x) { as.factor(x[-1]) } lapply(X=split(a, a[,1]), FUN=FUN) Best, Rasmus
Rui Barradas
2020-Apr-09 17:50 UTC
[R] create a r list from dataframe using the first column as list names
Hello, Your post is unreadable, please repost in *plain text*, not HTML. Rui Barradas ?s 16:00 de 09/04/20, aiguo li via R-help escreveu:> Hello allI need to create a r list with each row as a list object and named with the element in the first column.? Illustrated below:> a<- as.data.frame(matrix(LETTERS[1:16],nrow = 4))> a? V1 V2 V3 V41 ?A ?E ?I ?M2 ?B ?F ?J ?N3 ?C ?G ?K ?O4 ?D ?H ?L ?P > I want the list looks like$A[1] E I MLevels: E I M > $B[1] F J NLevels: F J N > $C[1] G K OLevels: G K O > $D[1] H L PLevels: H L P > > I used the script below, it does not work the way I wantlapply(split(a,a$V1), function(x) as.list(a[-1])) > any help would be greatly appreciated! > Anna > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
Rasmus Liland
2020-Apr-09 18:21 UTC
[R] create a r list from dataframe using the first column as list names
On 2020-04-09 18:50 +0100, Rui Barradas wrote: | Hello, | | Your post is unreadable, please repost in | *plain text*, not HTML. Hi! It was not so bad? I was able to extract out the core parts at least to prepare an answer ... maybe a bit hard with no line breaks, but ... Best, Rasmus
Rasmus Liland
2020-Apr-09 18:23 UTC
[R] create a r list from dataframe using the first column as list names
On 2020-04-09 18:00 +0000, aiguo li wrote: | That is awesome! Thanks. I'm glad this was helpful for you!
Rasmus Liland
2020-Apr-10 16:14 UTC
[R] create a r list from dataframe using the first column as list names
On 2020-04-09 18:00 +0000, aiguo li wrote: | That is awesome! Thanks. Dear AiGuo, I thought: why make this overly complicated, when this is also possible: a <- matrix(LETTERS[1:16], nrow=4) X <- split(x=a[,-1], f=a[,1]) lapply(X=X, FUN=as.factor) Best, Rasmus
Bert Gunter
2020-Apr-10 18:07 UTC
[R] create a r list from dataframe using the first column as list names
"I thought: why make this overly complicated,..." Indeed, though "complicated" is in the eyes of the beholder. One wonders whether any of this is necessary, though: see ?apply , as in apply(a, 1, whatever...) to do things rowwise. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Apr 10, 2020 at 9:14 AM Rasmus Liland <jensrasmus at gmail.com> wrote:> On 2020-04-09 18:00 +0000, aiguo li wrote: > | That is awesome! Thanks. > > Dear AiGuo, > > I thought: why make this overly > complicated, when this is also > possible: > > a <- matrix(LETTERS[1:16], nrow=4) > X <- split(x=a[,-1], f=a[,1]) > lapply(X=X, FUN=as.factor) > > Best, > Rasmus > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]