user1351353
user1351353

Reputation: 11

R Extract all values of a dataframe and combine it to a single list

I have following data frame:

     PD4115a PD4088a  PD4192a      
   1 ABCA8   ATRX     ADAM32      
   2 ANK2    CDH9     C11orf30
   3 ANO3    CKAP2L   CCBL2      

What I want is a list with all values combined:

     1 ABCA8 ATRX ADAM32 ANK2 CDH9 C11orf30 ANO3CKAP2L CCBL2

Please help me with that, I am a noob in R.

Many Thanks

Upvotes: 1

Views: 2346

Answers (1)

Josh O'Brien
Josh O'Brien

Reputation: 162321

Since data.frames are "really" a special kind of list, you can just use unlist(), like this:

df <- data.frame(A=letters[1:3], B=letters[4:6], stringsAsFactors = FALSE)
unlist(df)
#  A1  A2  A3  B1  B2  B3 
# "a" "b" "c" "d" "e" "f" 

## To help understand why it works, here are a few ways to see the sense 
## in which data.frames are lists 
is.list(df)         # Ask R
typeof(df)          # Check the storage mode
class(unclass(df))  # Strip off the "data.frame" class attribute

Upvotes: 2

Related Questions