R语言 如何将变量A的n个唯一值pivot_wide按变量B分组?

pvcm50d1  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(136)

我正在尝试pivot_wider()一个包含不同人名的数据框的列X。在group_by() df的另一个变量Y中,总是有2个这样的名字。我希望R在Y的每个唯一标识符中取2个唯一的X名称值,并将它们放在2个新列ex_X_Name_1和ex_X_Name_2中。
我的数据框如下所示:

df <- data.frame(Student = rep(c(17383, 16487, 17646, 2648, 3785), each = 2),
                 Referee = c("Paul Severe", "Cathy Nice", "Jean Exigeant", "Hilda Ehrlich", "John Rates",
                             "Eva Luates", "Fred Notebien", "Aldous Grading", "Hans Streng", "Anna Filaktic"),
                 Rating = format(round(x = sqrt(sample(15:95, 10, replace = TRUE)), digits = 3), nsmall = 3)
)

df

我想将Referee列转换为2个新列Referee_1和Referee_2,为每个学生分配2个唯一的Referee,并以以下结果结束:

even_row_df <- as.logical(seq_len(length(df$Referee)) %% 2)

df_wanted <- data_frame(
  Student = unique(df$Student),
  Referee_1 = df$Referee[even_row_df],
  Rating_Ref_1 = df$Rating[even_row_df],
  Referee_2 = df$Referee[!even_row_df],
  Rating_Ref_2 = df$Rating[!even_row_df]
)

df_wanted

我想我可以通过为学生/裁判组合的唯一行设置子集并进行连接来实现这一点,但是有没有一种方法可以在一次调用pivot_wideer中处理这一点呢?

jfgube3f

jfgube3f1#

您应该首先为每个组创建一个行ID:

library(dplyr)
library(tidyr)
df %>% 
  group_by(Student) %>% 
  mutate(row_n = row_number()) %>% 
  ungroup() %>% 
  pivot_wider(names_from = "row_n", values_from = c("Referee", "Rating"))

# A tibble: 5 × 5
  Student Referee_1     Referee_2      Rating_1 Rating_2
    <dbl> <chr>         <chr>          <chr>    <chr>   
1   17383 Paul Severe   Cathy Nice     9.165    7.810   
2   16487 Jean Exigeant Hilda Ehrlich  5.196    6.557   
3   17646 John Rates    Eva Luates     7.211    5.568   
4    2648 Fred Notebien Aldous Grading 4.000    8.124   
5    3785 Hans Streng   Anna Filaktic  7.937    6.325
5sxhfpxr

5sxhfpxr2#

使用数据.表

library(data.table)

setDT(df)

merge(df[, .SD[1], Student], df[, .SD[2], Student], by = "Student", suffixes = c("_1", "_2"))

# Student     Referee_1 Rating_1      Referee_2 Rating_2
# 1:    2648 Fred Notebien    6.708 Aldous Grading    9.747
# 2:    3785   Hans Streng    6.245  Anna Filaktic    8.775
# 3:   16487 Jean Exigeant    7.681  Hilda Ehrlich    4.359
# 4:   17383   Paul Severe    4.583     Cathy Nice    7.616
# 5:   17646    John Rates    6.708     Eva Luates    8.246

相关问题