我。输入 --> 输出
您可以简单地拆分数据框并再次绑定
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