将具有重复行的两列数据框扩展为 3 列

Spread two-columned dataframe with duplicated rows into 3 cols

我在R中有这种数据框

x       y
name1   value x
name1   value y
name2   value x
name2   value y
name3   value x
name3   value y

我想这样转换

x        y         z
name1   value x   value y
name2   value x   value y
name3   value x   value y

我尝试用 pivot_wider 重塑但没有成功。

df <- read.table(text = "x       y
name1   'value x'
name1   'value y'
name2   'value x'
name2   'value y'
name3   'value x'
name3   'value y'", header = T)

library(tidyverse)

df %>%
  group_by(dummy = cumsum(x != lag(x, default = 'NULL'))) %>%
  mutate(dummy = row_number()) %>%
  pivot_wider(id_cols = x, names_from = dummy, values_from = y)

# A tibble: 3 x 3
  x     `1`     `2`    
  <chr> <chr>   <chr>  
1 name1 value x value y
2 name2 value x value y
3 name3 value x value y

对于奇数 rows/even 行隔离

df %>%
  mutate(dummy = (row_number() +1) %% 2) %>%
  pivot_wider(id_cols = x, names_from = dummy, values_from = y)

# A tibble: 3 x 3
  x     `0`     `1`    
  <chr> <chr>   <chr>  
1 name1 value x value y
2 name2 value x value y
3 name3 value x value y

df %>%
  mutate(dummy = c('x_val', 'y_val')[1 +(row_number() +1) %% 2]) %>%
  pivot_wider(id_cols = x, names_from = dummy, values_from = y)

# A tibble: 3 x 3
  x     x_val   y_val  
  <chr> <chr>   <chr>  
1 name1 value x value y
2 name2 value x value y
3 name3 value x value y

这可能是您的 pivot_wider 选项

df %>%
  arrange(x, y) %>%
  group_by(x) %>%
  mutate(mn = c("y", "z")) %>%
  pivot_wider(names_from = mn, values_from = y) %>%
  ungroup()

这给出了

# A tibble: 3 x 3
  x     y       z      
  <chr> <chr>   <chr>
1 name1 value x value y
2 name2 value x value y
3 name3 value x value y

获取奇数行和偶数行,然后绑定列:

cbind(df[c(TRUE, FALSE), ], z = df[c(FALSE, TRUE), 2])
#       x       y       z
# 1 name1 value x value y
# 3 name2 value x value y
# 5 name3 value x value y