简体   繁体   English

根据评分者和主题重新排列数据,同时创建新的行名称

[英]Rearranging data according to rater and subject, simultaneously creating new row names

I have a dataset where multiple raters rate multiple subjects.我有一个数据集,其中多个评分者对多个主题进行评分。

I'd like to rearrange the data that looks like this:我想重新排列如下所示的数据:

data <- data.frame(rater=c("A", "B", "C", "A", "B", "C"),
                   subject=c(1, 1, 1, 2, 2, 2),
                   measurment1=c(1, 2, 3, 4, 5,6),
                   measurment2=c(11, 22, 33, 44, 55,66),
                   measurment3=c(111, 222, 333, 444, 555, 666))

data
#     rater   subject  measurment1 measurment2 measurment3
# 1     A       1           1          11         111
# 2     B       1           2          22         222
# 3     C       1           3          33         333
# 4     A       2           4          44         444
# 5     B       2           5          55         555
# 6     C       2           6          66         666

into data that looks like this:变成如下所示的数据:

data_transformed <- data.frame( A = c(1,11,111,4,44,444),
                                B = c(2,22,222,5,55,555),
                                C = c(3,33,333,6,66,666) 
)

row.names(data_transformed) <- c("measurment1_1", "measurment2_1", "measurment3_1", "measurment1_2", "measurment2_2", "measurment3_2") 

data_transformed
#                 A   B   C
# measurment1_1   1   2   3
# measurment2_1  11  22  33
# measurment3_1 111 222 333
# measurment1_2   4   5   6
# measurment2_2  44  55  66
# measurment3_2 444 555 666

In the new data frame, the raters (A, B and C) should become the columns.在新数据框中,评分者(A、B 和 C)应成为列。 The measurement should become the rows and I'd also like to add the subject number as a suffix to the row-names.测量值应成为行,我还想将主题编号添加为行名的后缀。

For the rearranging one could probably use the pivot functions, yet I have no idea on how to combine the measurement-variables with the subject number.对于重新排列,可能可以使用 pivot 函数,但我不知道如何将测量变量与主题编号结合起来。

Thanks for your help!谢谢你的帮助!

We could use pivot_longer , pivot_wider and unite from the tidyr package.我们可以使用pivot_longer中的tidyrpivot_widerunite

pivot_longer makes our data in a vertical format, it transforms the measurment columns into a sigle variable pivot_longer使我们的数据处于垂直格式,它将测量列转换为单个变量

pivot_wider does the opposite of pivot_longer, transform a variable into multiple columns for each unique value from the variable pivot_wider与 pivot_longer 相反,为变量中的每个唯一值将变量转换为多列

  data |> 
      pivot_longer(measurment1:measurment3) |> 
      pivot_wider(names_from = rater, values_from = value, values_fill = 0 ) |> 
      unite("measure_subjet",name,subject, remove = TRUE)

Please try the below code where we can accomplish the expected result using pivot_longer , pivot_wider and column_to_rownames .请尝试下面的代码,我们可以使用pivot_longerpivot_widercolumn_to_rownames完成预期的结果。

library(tidyverse)

data_transformed <- data %>% 
  pivot_longer(c('measurment1', 'measurment2', 'measurment3')) %>% 
  mutate(rows = paste0(name, '_', subject)) %>% 
  pivot_wider(rows, names_from = rater, values_from = value) %>% 
  column_to_rownames(var = "rows")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM