【问题标题】:Merge two columns into duplicate row in R将两列合并为R中的重复行
【发布时间】:2021-11-27 14:21:28
【问题描述】:

我知道pivot_longer 虽然这让我很困惑,因为我不确定我应该在values_to 参数中添加什么。下面的图片几乎就是我所需要的

代码:

df <- structure(list(A = 1:3, B = c("a", "b", "c"), C = c("d", "e", 
"f"), D = c(10L, 15L, 20L), E = c(20L, 30L, 40L)), class = "data.frame", row.names = c(NA, 
-3L))

我的尝试:

library(tidyverse)
df %>% pivot_longer(cols=3,names_to="new",values_to="value")

** 如果您还可以添加如何从输出转换为输入,那将非常有帮助。

我应该改用bind_rows() 吗?

【问题讨论】:

  • df %&gt;% pivot_longer(cols = B:C) %&gt;% select(-name)

标签: r dplyr tidyverse tidyr


【解决方案1】:

我。输入 --> 输出

您可以简单地拆分数据框并再次绑定

library(dplyr)

df1 <- df %>% select(A, B, D, E)
df2 <- df %>% select(A, C, D, E) %>% rename(B = C)

bind_rows(df1, df2)
  A B  D  E
1 1 a 10 20
2 2 b 15 30
3 3 c 20 40
4 1 d 10 20
5 2 e 15 30
6 3 f 20 40

如果你想使用 pivot_longer,你可以这样做:

library(tidyr)

df %>% 
  pivot_longer( cols = B:C                  # the cols we want to combine
              , names_to = "old_col_names"  # the col where we store the old names
              , values_to = "B"             # and we rename the new col to B
              ) %>% 
# ------------- reoder columns and arrange to make it look like output
   select(A, B, D, E) %>%                   # this removes the 'old_col_names' you could also do select(-old_col_names)
   arrange(B)                               # arrange in alphabetical order

II 输出 --> 输入

对于反向操作 - 假设您不想拆分 df 并重新组合它 - 您可以使用 {tidyr}pivot_wider() 使用符合您要求的“新”名称列。对于更复杂的数据集,您可能需要在这里发挥创造力。

library(tidyr)

output %>% 
# --------- introduce a "name" vector -------------
## -------- we use rep() to create set of 3s ... adapt as required!
  mutate(group = c(rep("B",3), rep("C",3)) ) %>% 
# --------- spread the data frame and rearrange the columns
  pivot_wider( id_cols = c(A,D,E)    # these columns are "constant"
             , names_from  = group   # pull "new" column names from our group var 
             , values_from = B) %>%  # spread the values we aggregated in B
  select(A, B, C, D, E)              # rearrange column order to input style

【讨论】:

    猜你喜欢
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2020-11-01
    • 2019-09-22
    相关资源
    最近更新 更多