将 data.frame 列更改为 R 中的行

发布于 2024-12-03 14:53:13 字数 266 浏览 1 评论 0原文

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
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

I would like to have this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚你心 2024-12-10 14:53:13

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

data.frame(t(df))

不过,最好从一开始就按照你想要的方式定义它

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))

如果你想给列名称,你有时

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

,如果我使用 Excel 数据的 read.DIF,数据就会被转置。你就是这样获取原始数据的吗?如果是这样,您可以致电

read.DIF(filename, transpose = T)

以获取正确方向的数据。

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))

You could also do this

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))

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.

北渚 2024-12-10 14:53:13

我真的推荐没有手动步骤的 data.table 方法,因为它们很容易出错

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

library('data.table')
dat.m <- melt(as.data.table(df, keep.rownames = "Vars"), id.vars = "Vars") # https://stackoverflow.com/a/44128640/54964

dat.m

输出

  A B C D E
1 1 2 3 4 5
2 6 7 8 9 0
    Vars variable value
 1:    1        A     1
 2:    2        A     6
 3:    1        B     2
 4:    2        B     7
 5:    1        C     3
 6:    2        C     8
 7:    1        D     4
 8:    2        D     9
 9:    1        E     5
10:    2        E     0

R:3.4.0(向后移植)
操作系统:Debian 8.7

I really recommend data.table approach without manual steps becauce they are error-prone

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

library('data.table')
dat.m <- melt(as.data.table(df, keep.rownames = "Vars"), id.vars = "Vars") # https://stackoverflow.com/a/44128640/54964

dat.m

Output

  A B C D E
1 1 2 3 4 5
2 6 7 8 9 0
    Vars variable value
 1:    1        A     1
 2:    2        A     6
 3:    1        B     2
 4:    2        B     7
 5:    1        C     3
 6:    2        C     8
 7:    1        D     4
 8:    2        D     9
 9:    1        E     5
10:    2        E     0

R: 3.4.0 (backports)
OS: Debian 8.7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文