本文介绍了将一行数据帧转换为R中的一个简单向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的数据框架,我只从中选择几行。然后根据条件删除一些列。让我们说我选择行4460,如下所示:

  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

问题是我需要将此行转换为一个简单的向量(意思是我应该摆脱所有列/行名称),以便我可以将其传递给另一个函数。我想要得到以下结果:

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

我尝试了 as.list as.vector ,但没有人给出我期待的结果。任何想法?

解决方案

mtcars 数据

b
$ b

  mydata< -mtcars 
k< -mydata [1,]
mpg cyl disp hp drat wt qsec vs am gear carb
马自达RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
名称(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

根据@Ananda更新: unlist(mydata [1,],use.names = FALSE)


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?

解决方案

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)

这篇关于将一行数据帧转换为R中的一个简单向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:08