【发布时间】:2021-06-21 15:16:34
【问题描述】:
全部。
对 R 非常陌生,并且可以更广泛地处理数据。也从未在 Stack Overflow 或类似内容中写过任何东西,因此请原谅任何不可靠的格式或术语。
我有一个需要从长格式转换为宽格式的数据框,为配对 T 检验做好准备。数据框为:
> head(df)
# A tibble: 6 x 13
Assessor Product Ap Ar F T
<dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 MC 10 10 10 10
2 1 MV 10 10 10 10
3 2 MC 6 7 8 8
4 2 MV 7 5 4 8
5 3 MV 9 9 10 9
6 3 MC 6 8 7 6
# ... with 7 more variables:
# C1 <dbl>,
# JCo <dbl>,
# JSt1 <dbl>,
# JSt2 <dbl>,
# JSw <dbl>,
# JCr <dbl>,
# OA <dbl>
我需要将它放在以下宽格式中,但对于所有变量,其中 Product 之后的每一列都转换为两列,每个产品一列(MC 和 MV):
Assessor Ap_MC Ap_MV Ar_MC Ar_MV
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 10 10 10 10
2 2 6 7 7 5
3 3 9 6 9 8
标题不一定是这些,但需要区分它们是按不同产品以及原始列分组的。
我在这里看到了各种在长格式和宽格式之间转换的方法,但我无法进行任何工作。这是我使用 pivot_wider() 所做的尝试(包括错误消息):
df <- pivot_wider(df, id_cols = df[1], names_from = df[2], values_from = df[3:13])
Error: Must subset columns with a valid subscript vector.
x Subscript has the wrong type `tbl_df<Product:character>`.
i It must be numeric or character.
Run `rlang::last_error()` to see where the error occurred.
我不知道是什么导致了这个错误,更不用说我的尝试是否接近给我想要的输出了。
提前谢谢你!
【问题讨论】:
-
我认为这里已经回答了这个问题:stackoverflow.com/questions/68043121/…
标签: r