Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a list of specific variables of a dataframe?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading