Skip to content

create a r list from dataframe using the first column as list names

4 messages · aiguo li, Rui Barradas, Rasmus Liland

#
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
#
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
#
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:
#
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