将两列合并到 R 中的重复行中
Merge two columns into duplicate row in R
我知道 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()
吗?
我。输入 --> 输出
您可以简单地拆分您的数据框并再次绑定它
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
我知道 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()
吗?
我。输入 --> 输出
您可以简单地拆分您的数据框并再次绑定它
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