如何更改R中数据框的形状?(将具有相同名称的列堆叠在一起) [英] How to change the shape of a data frame in R? (stacking columns with the same names together)

查看:52
本文介绍了如何更改R中数据框的形状?(将具有相同名称的列堆叠在一起)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 R 中重塑数据框:

I'm trying to reshape a data frame in R:

  Gene_ID Value Gene_ID.1 Value.1 Gene_ID.2 Value.2
1       A     0         A       3         A       1
2       B     5         B       6         B       5
3       C     7         C       2         C       7
4       D     8         D       9         D       2
5       E     5         E       8         E       4
6       F     6         F       4         F       5

我想让它看起来像这样:

I want to make it look like this:

   Gene_ID Value
1        A     0
2        B     5
3        C     7
4        D     8
5        E     5
6        F     6
7        A     1
8        B     5
9        C     7
10       D     2
11       E     4
12       F     5
13       A     3
14       B     6
15       C     2
16       D     9
17       E     8
18       F     4

因此,只需将具有相同名称的列堆叠在一起即可.有没有办法这样做?谢谢!

So simply stack the columns with the same names together. Is there a way to do so? Thanks!

推荐答案

您可以使用 gather()/spread()pivot_longer() 来自 tidyr 包.

You can use either the combination of gather()/spread() or pivot_longer() from the tidyr package.

要了解有关新 pivot_xxx() 函数的更多信息,请查看以下链接:

To learn more about the new pivot_xxx() functions, check out these links:

在 tidyverse 中将数据从列转换为行(然后返回!)

library(dplyr)
library(tidyr)

txt <- "  Gene_ID.0 Value.0 Gene_ID.1 Value.1 Gene_ID.2 Value.2
1       A     0         A       3         A       1
2       B     5         B       6         B       5
3       C     7         C       2         C       7
4       D     8         D       9         D       2
5       E     5         E       8         E       4
6       F     6         F       4         F       5"

dat <- read.table(text = txt, header = TRUE)

组合gather()separate()spread() 函数

dat %>%
  mutate(Row_Nr = row_number()) %>%
  gather(key, value, -Row_Nr) %>%
  separate(key, into = c("key", "Gene_Nr"), sep = "\\.") %>% 
  spread(key, value) %>%
  select(-Row_Nr)
#> Warning: attributes are not identical across measure variables;
#> they will be dropped
#>    Gene_Nr Gene_ID Value
#> 1        0       A     0
#> 2        1       A     3
#> 3        2       A     1
#> 4        0       B     5
#> 5        1       B     6
#> 6        2       B     5
#> 7        0       C     7
#> 8        1       C     2
#> 9        2       C     7
#> 10       0       D     8
#> 11       1       D     9
#> 12       2       D     2
#> 13       0       E     5
#> 14       1       E     8
#> 15       2       E     4
#> 16       0       F     6
#> 17       1       F     4
#> 18       2       F     5

使用pivot_longer()

### gather all values columns
### separate original column names by the period "."
### into Gene_ID/Value and Gene_Nr
dat %>% 
  pivot_longer(everything(),
               names_to = c(".value", "Gene_Nr"),
               names_pattern = "(.*)\\.(.*)")
#>    Gene_Nr Gene_ID Value
#> 1        0       A     0
#> 2        1       A     3
#> 3        2       A     1
#> 4        0       B     5
#> 5        1       B     6
#> 6        2       B     5
#> 7        0       C     7
#> 8        1       C     2
#> 9        2       C     7
#> 10       0       D     8
#> 11       1       D     9
#> 12       2       D     2
#> 13       0       E     5
#> 14       1       E     8
#> 15       2       E     4
#> 16       0       F     6
#> 17       1       F     4
#> 18       2       F     5

reprex 包 (v0.3.0) 于 2019 年 12 月 8 日创建

Created on 2019-12-08 by the reprex package (v0.3.0)

这篇关于如何更改R中数据框的形状?(将具有相同名称的列堆叠在一起)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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