Skip to content
Prev 383210 / 398502 Next

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