将data.frame列更改为R中的行 [英] change data.frame column into rows in R

查看:56
本文介绍了将data.frame列更改为R中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A <- c(1,6)
B <- c(2,7)
C <- c(3,8)
D <- c(4,9)
E <- c(5,0)
df <- data.frame(A,B,C,D,E)
df
  A B C D E
1 1 2 3 4 5
2 6 7 8 9 0

我想要这个:

df
   1  2
A  1  6 
B  2  7
C  3  8
D  4  9    
E  5  0


推荐答案

如果您的数据框确实是这种格式,则所有向量都将是字符向量。或者,您基本上有一个字符矩阵,可以这样做:

If your dataframe is truly in that format, then all of your vectors will be character vectors. Or, you basically have a character matrix and you could do this:

data.frame(t(df))

不过,最好只是从一开始就定义它

It would be better, though, to just define it the way you want it from the get-go

df <- data.frame(c('A','B','C','D','E'), 
                 c(1, 2, 3, 4, 5),
                 c(6, 7, 8, 9, 0))

您也可以这样做

df <- data.frame(LETTERS[1:5], 1:5, c(6:9, 0))

如果您想给列名命名,您可以这样做

If you wanted to give the columns names, you could do this

df <- data.frame(L = LETTERS[1:5], N1 = 1:5, N2 = c(6:9, 0))

有时,如果我使用Excel数据的read.DIF,则数据会转置。这是您获取原始数据的方式吗?如果是这样,您可以调用

Sometimes, if I use read.DIF of Excel data the data gets transposed. Is that how you got the original data in? If so, you can call

read.DIF(filename, transpose = T)

以正确的方向获取数据。

to get the data in the correct orientation.

这篇关于将data.frame列更改为R中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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