Home > Software engineering >  How to make a list of specific variables of a dataframe?
How to make a list of specific variables of a dataframe?

Time:11-06

I have this dataframe (but let's imagine it very big)

df = data.frame(x = c(1,0,0,0,1,1,1,NA), y = c(2,2,2,2,3,3,2,NA),
                z = c(1:7,NA), m = c(1,2,3,1,2,3,1,NA) )

df$x = factor(df$x)
df$y = factor(df$y)
df$m = factor(df$m)

and I wish to create a list that looks like as follows

l1 = list(df$x,df$y,df$z,df$m)

with the resulted output as follows :

[[1]]
[1] 1    0    0    0    1    1    1    <NA>
Levels: 0 1

[[2]]
[1] 2    2    2    2    3    3    2    <NA>
Levels: 2 3

[[3]]
[1]  1  2  3  4  5  6  7 NA

[[4]]
[1] 1    2    3    1    2    3    1    <NA>
Levels: 1 2 3

would appreciate the help

CodePudding user response:

Use as.list. "data.frame"s are actually lists, we just need to remove the "data.frame" class to get the underlying "list".

as.list(df)
# $x
# [1] 1    0    0    0    1    1    1    <NA>
#   Levels: 0 1
# 
# $y
# [1] 2    2    2    2    3    3    2    <NA>
#   Levels: 2 3
# 
# $z
# [1]  1  2  3  4  5  6  7 NA
# 
# $m
# [1] 1    2    3    1    2    3    1    <NA>
#   Levels: 1 2 3

Or unclass it from data.frame

unclass(df)
# $x
# [1] 1    0    0    0    1    1    1    <NA>
#   Levels: 0 1
# 
# $y
# [1] 2    2    2    2    3    3    2    <NA>
#   Levels: 2 3
# 
# $z
# [1]  1  2  3  4  5  6  7 NA
# 
# $m
# [1] 1    2    3    1    2    3    1    <NA>
#   Levels: 1 2 3
# 
# attr(,"row.names")
# [1] 1 2 3 4 5 6 7 8
  • Related