Using R convert data.frame to simple vector

后端 未结 3 835
醉话见心
醉话见心 2020-12-03 11:35

I have this data.frame:

> print(v.row)
    X1  X2  X3  X4  X5  X6  X7  X8  X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24
57          


        
相关标签:
3条回答
  • 2020-12-03 11:52

    I had this data frame from csv

    x <- as.numeric(dataframe$column_name)
    

    worked great. (same with dataframe[3], 3 being my column index didn't work)

    0 讨论(0)
  • 2020-12-03 11:56

    see ?unlist

    Given a list structure x, unlist simplifies it to produce a vector which contains all the atomic components which occur in x.

    unlist(v.row)
    [1] 177 165 177 177 177 177 145 132 126 132 132 132 126 120 145 167 167 167
           167 165 177 177 177 177
    

    EDIT

    You can do it with as.vector also, but you need to provide the correct mode:

     as.vector(v.row,mode='numeric')
     [1] 177 165 177 177 177 177 145 132 126 132 132 132 126 120 145 167 167
          167 167 165 177 177 177 177
    
    0 讨论(0)
  • Just a note on the second part of agstudy's answer:

    df <- data.frame(1:10) 
    as.vector(df, mode="integer") #Error
    
    as.vector(df[[1]],mode="integer") #Works; 
    as.vector(df[[1]]) #Also works
    

    i.e., apparently you have to select the list element from the dataframe even though there's only 1 element.

    0 讨论(0)
提交回复
热议问题