将data.frame折叠成向量 [英] Collapse a data.frame into a vector

查看:100
本文介绍了将data.frame折叠成向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数据框:

I have a data frame like this:

COL1  COL2  COL3 

   a           h
   b     f    
   c     g       
   d           j

我想要以下输出:

 COL  
   a    
   b    
   c    
   d   
   f   
   g   
   h   
   j   

该怎么做?
预先感谢!

How this can be done? Thanks in advance!

推荐答案

如果您的 data.frame 命名为 dat ,请尝试:

If your data.frame is named dat, try:

unlist(dat, use.names=FALSE)

您可能还想尝试以下操作:

You may also want to try the following:

as.character(unlist(dat, use.names=FALSE))
c(na.omit(as.character(unlist(dat, use.names=FALSE))))

如果将数据作为要素输入,这可能会有用。

which might be useful if your data are entered as factors.

另外,在您的问题中,您似乎似乎并没有一个基本的向量,而是一个单列数据。框架。因此,类似 dat2<-data.frame(COL = c(na.omit(as.character(unlist(dat,use.names = FALSE))))))可能就是您要找的东西。

Also, in your question, you make it appear that you don't actually have a basic "vector", but rather a one-column data.frame. Thus, something like dat2 <- data.frame(COL = c(na.omit(as.character(unlist(dat, use.names=FALSE))))) might be what you're looking for.

这篇关于将data.frame折叠成向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆