Convert data frame string to simple vector in R

I have a huge data frame from which I select only a couple of rows. Then I delete some columns based on the condition. let's say that I select line 4460 as shown below:

        V1870 V107 V1315 V1867 V1544 V1207 V1252 V1765 V342 V429 V1826 V865 V1374
4460     0    0     3     0     5     0     2     0    4    0     0    0     0

The problem is that I need to convert this row to a simple vector (which means that I have to get rid of all the column / row names) in order to pass it to another function. I would like to have the following result:

    [1] 0 0 3 0 5 0 2 0 4 0 0 0 0

I tried as.listand as.vector, but none of them gave the expected results. Any idea?

0
source share
3 answers

mtcarsData example

mydata<-mtcars
k<-mydata[1,]
          mpg cyl disp  hp drat   wt  qsec vs am gear carb
Mazda RX4  21   6  160 110  3.9 2.62 16.46  0  1    4    4
names(k)<-NULL

unlist(c(k))
 [1]  21.00   6.00 160.00 110.00   3.90   2.62  16.46   0.00   1.00   4.00   4.00

Updated according to @Ananda: unlist(mydata[1, ], use.names = FALSE)

+1

, , ( data.frame).

    #random df
    DF = data.frame(col1 = sample(1:10, 10), col2 = sample(1:10,10), col3 = sample(1:10, 10))

    DF[5,]
      col1 col2 col3
    5    3    7    5

    mode(DF[5,]) 
    [1] "list"

    mode(unlist(DF[5,]))
    [1] "numeric"

    names(DF[5,])
    [1] "col1" "col2" "col3"

    sum(DF[5,]) # computations are naturally done
    [1] 15

, , :

    unname(unlist(DF[5,]))
    [1] 3 7 5
+1

Try using the row index.

eq

list <- yourdataframe[4460, ]
0
source

Source: https://habr.com/ru/post/1663030/


All Articles