Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

list - convert a row of a data frame to a simple vector in R

I have a huge data frame from which I only select a couple of rows. Then I remove some of the columns based on a condition. let us say that I choose row 4460 as shown bellow:

        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 ( meaning I should get rid of all the column/row names) so that I can pass it to another function. I would like to be able to have the following result:

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

I tried as.list and as.vector, but none of them gave the results I was expecting. Any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Example from mtcars data

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 as per @Ananda: unlist(mydata[1, ], use.names = FALSE)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...