R语言 从宽格式转换为长格式时保留列顺序

hkmswyz6  于 2023-02-01  发布在  其他
关注(0)|答案(6)|浏览(161)

当我从宽格式到长格式收集列时,我试图保持列的顺序。我遇到的问题是在我输入gathersummarize之后,顺序丢失了。列的数量非常大,所以我不想手动输入顺序。
下面是一个例子:

library(tidyr)
library(dplyr)

N <- 4
df <- data.frame(sample = c(1,1,2,2),
                 y1.1 = rnorm(N), y2.1 = rnorm(N), y10.1 = rnorm(N))
> df
  sample      y1.1      y2.1      y10.1
1      1  1.040938 0.8851727 -0.3617224
2      1  1.175879 1.0009824 -1.1352406
3      2 -1.501832 0.3446469 -1.8687008
4      2 -1.326817 0.4434628 -0.8795962

我想要的是保持列的顺序。在我做了一些操作之后,顺序丢失了。如下图所示:

dfg <- df %>% 
  gather(key="key", value="value", -sample) %>%
  group_by(sample, key) %>%
  summarize(mean = mean(value))

> filter(dfg, sample == 1)
  sample   key       mean
   <dbl> <chr>      <dbl>
1      1  y1.1  0.2936335
2      1 y10.1  0.6170505
3      1  y2.1 -0.2250543

你可以看到它是如何把y10.1放在y2.1前面的,我不想这样,我想保持这个顺序,如下图所示:

dfg <- df %>% 
  gather(key="key", value="value", -sample)

> filter(dfg, sample == 1)
  sample   key       value
1      1  y1.1  0.60171521
2      1  y1.1 -0.01444823
3      1  y2.1  0.81566726
4      1  y2.1 -1.26577581
5      1 y10.1  0.41686388
6      1 y10.1  0.81723707

由于某种原因,group_bysummarize操作改变了顺序。我不知道为什么。我尝试了ungroup命令,但没有任何效果。正如我前面所说的,我的实际数据框有许多列,我需要保持顺序。保持顺序的原因是我可以按照正确的顺序绘制数据。
有什么想法吗?

oxf4rvwz

oxf4rvwz1#

或者,您可以将键列转换为具有反映原始列名顺序的水平的因子:

df %>% 
    gather(key="key", value="value", -sample) %>%
    mutate(key=factor(key, levels=names(df)[-1])) %>% # add this line to convert the key to a factor
    group_by(sample, key) %>%
    summarize(mean = mean(value)) %>%
    filter(sample == 1)

# A tibble: 3 x 3
# Groups:   sample [1]
#  sample    key       mean
#   <dbl> <fctr>      <dbl>
#1      1   y1.1  0.8310786
#2      1   y2.1 -1.2596933
#3      1  y10.1  0.8208812
7rfyedvj

7rfyedvj2#

tidyverse包现在支持优雅的解决方案:

library(tidyverse)
    N <- 4
    df <- data.frame(sample = c(1,1,2,2),
                    y1.1 = rnorm(N), y2.1 = rnorm(N), y10.1 = rnorm(N))
    df %>% 
        gather("key", "value", -sample, factor_key = T) %>% 
        group_by(sample, key) %>%
        summarise(mean = mean(value))

这导致了

# A tibble: 6 x 3
    # Groups:   sample [2]
    sample key      mean
    <dbl> <fct>   <dbl>
    1      1 y1.1   0.0894
    2      1 y2.1   0.551 
    3      1 y10.1  0.254 
    4      2 y1.1  -0.555 
    5      2 y2.1  -1.36  
    6      2 y10.1 -0.794
wyyhbhjk

wyyhbhjk3#

我通过使用查找表找到了一个可行的解决方案。它似乎对我很有效,因为我可以提取列名并为列名分配一个有序编号,然后与我的data.frame配对。
解决方案如下:

lookup <- tibble(key = c("y1.1", "y2.1", "y10.1"),
                 index = c(1,2,3))

> left_join(dfg, lookup, by="key")
# A tibble: 6 x 4
  sample   key       mean index
   <dbl> <chr>      <dbl> <dbl>
1      1  y1.1  0.2936335     1
2      1 y10.1  0.6170505     3
3      1  y2.1 -0.2250543     2
4      2  y1.1  1.3652070     1
5      2 y10.1  0.9889233     3
6      2  y2.1  0.5216553     2
p5fdfcr1

p5fdfcr14#

如果您的列确实是按照它所包含的数字排序的,这应该可以工作:

library(readr)

df %>% 
  gather(key="key", value="value", -sample) %>%
  group_by(sample, key)         %>%
  summarize(mean = mean(value)) %>%
  arrange(parse_number(key))    %>%  # <- sorting by number contained in key
  filter(sample == 1)

# # A tibble: 3 x 3
# # Groups:   sample [1]
#     sample   key       mean
# <dbl> <chr>      <dbl>
#   1      1  y1.1 -0.9236688
#   2      1  y2.1 -0.2168337
#   3      1 y10.1  0.5041981
ryoqjall

ryoqjall5#

还有一种方法是使用您想要排序的键列的定制版本来arrange Dataframe :

library(dplyr)
library(tidyr)

df %>% 
  gather(key="key", value="value", -sample) %>%
  group_by(sample, key) %>%
  summarize(mean = mean(value)) %>%
  arrange(as.numeric(stringr::str_replace(key, "y", "")), .by_group = TRUE)

#> # A tibble: 6 x 3
#> # Groups:   sample [2]
#>   sample   key        mean
#>    <dbl> <chr>       <dbl>
#> 1      1  y1.1  0.07001689
#> 2      1  y2.1  1.15349430
#> 3      1 y10.1  1.18266024
#> 4      2  y1.1  0.42616604
#> 5      2  y2.1  1.05891682
#> 6      2 y10.1 -0.12561209
iih3973s

iih3973s6#

如果我们合并前面建议答案的思想,并且使用pivot_longer(),因为它不过时,我们可以添加一个步骤来设置键as_factor()而不是类字符。如果我们将其保留为字符,它将被重新排序为字母数字顺序。

library(tidyverse)

N <- 4
df <- data.frame(sample = c(1,1,2,2),
                 y1.1 = rnorm(N), y2.1 = rnorm(N), y10.1 = rnorm(N))

dfg <- df |> 
  pivot_longer(2:4, names_to = "key", values_to = "value") |> 
  mutate(key = as_factor(key)) |> 
  group_by(sample, key) |> 
  summarize(mean = mean(value)) |> 
  ungroup()

dfg

# A tibble: 6 × 3
  sample key     mean
   <dbl> <fct>  <dbl>
1      1 y1.1  -0.789
2      1 y2.1   1.16 
3      1 y10.1 -0.187
4      2 y1.1   0.962
5      2 y2.1   0.673
6      2 y10.1  0.502

相关问题