按行而不是列取消列出数据框 [英] Unlist a data frame by rows, not columns

查看:84
本文介绍了按行而不是列取消列出数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个相对简单的问题,但是答案似乎使我难以理解。当前,我有一个类似于以下内容的数据框:

A relatively simple question, but the answer seems to have eluded me. Currently, I have a data frame which looks similar to this:

0   0   0   1   1
0   1   0   1   1
2   1   1   0   3

我正在尝试将其变成一行数据(按行)。我使用了 unlist 函数,它实现了我想要的功能,但是按列将其提供给我。它给了我这个:

I'm trying to turn this into a single line of data, by rows. I used the unlist function, and it did what I wanted, but gave them to me by columns. It gave me this:

0,0,2,0,1,1,0,0,1,1,1,0,1,1,3

但是我想要的是:

0,0,0,1,1,0,1,0,1,1,2,1,1,0,3

我很抱歉,这似乎是一个愚蠢的问题,但我仍然是R的新手。任何帮助(或

I apologize if this seems like a silly question, but I'm still a novice with R. Any help (or referrals to functions which might help me process this) would be greatly appreciated.

推荐答案

我们可以进行转置( t ),然后使用 c 获得矢量输出

We can take the transpose (t) of the dataset and then use c to get a vector output

 c(t(df1))
 #[1] 0 0 0 1 1 0 1 0 1 1 2 1 1 0 3

通过转置,我们将'data.frame'转换为'matrix'。在 data.frame matrix 中, unlist / c 操作按列进行。因此,转置将列换为行,反之亦然,我们得到了预期的结果。

By doing transpose, we convert the 'data.frame' to 'matrix'. In both data.frame or matrix, unlist/c operations happen columnwise. So, transposing swaps the columns for rows and viceversa and we get the expected result.

这篇关于按行而不是列取消列出数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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