gpt4 book ai didi

r - 使用 tidyverse 有条件地转置选择行

转载 作者:行者123 更新时间:2023-12-02 00:37:02 25 4
gpt4 key购买 nike

我正在处理一个数据集,并尝试使用 tidyverse 对其进行 reshape 。

来自:

|Name |eval   |test |type      | score|
|:----|:------|:----|:---------|-----:|
|John |first |1 |pretest | 10|
|John |first |1 |posttest | 15|
|John |first |2 |pretest | 20|
|John |first |2 |posttest | 30|
|John |second |1 |pretest | 35|
|John |second |1 |posttest | 50|
|John |second |2 |pretest | 5|
|John |second |2 |posttest | 10|
|Jane |first |1 |pretest | 40|
|Jane |first |1 |posttest | 20|
|Jane |first |2 |pretest | 10|
|Jane |first |2 |posttest | 20|

致:

|Name |eval   |new_name      | pre_test| post_test|
|:----|:------|:-------------|--------:|---------:|
|John |first |John_first_1 | 10| 15|
|John |first |John_first_2 | 20| 30|
|John |second |John_second_1 | 35| 50|
|John |second |John_second_2 | 5| 10|
|Jane |first |Jane_first_1 | 40| 20|
|Jane |first |Jane_first_2 | 10| 20|
  • 尝试执行group_by,以便按名称进行分组、评估和测试,以便每个组本质上都是针对特定人员的 pre_test 与 post_test。

  • 还尝试对名称、评估、测试和类型使用unite。但如果我在那之后进行传播,那么每个唯一的名称最终都会成为许多列。

  • 还尝试先对名称进行统一,然后进行评估、测试,然后使用 key=(新的统一名称) 和 value = 进行传播值,但输出不是我想要的

我知道可以编写一个循环函数来获取所有其他值并将其放入一个新列中,但我正在尝试查看是否有一种 tidyverse 方法来解决此问题。

谢谢!!

library(tidyverse)
Name <- c('John', 'John', 'John', 'John',
'John', 'John', 'John', 'John',
'Jane', 'Jane', 'Jane', 'Jane')
eval <- c('first', 'first', 'first', 'first',
'second', 'second', 'second', 'second',
'first', 'first', 'first', 'first')
test <- c('1', '1', '2', '2',
'1', '1', '2', '2',
'1', '1', '2', '2')
type <- c('pretest', 'posttest', 'pretest', 'posttest',
'pretest', 'posttest', 'pretest', 'posttest',
'pretest', 'posttest', 'pretest', 'posttest')
score <- c(10, 15, 20, 30, 35, 50, 5, 10, 40, 20, 10, 20)
df <- data.frame(Name, eval, test, type, score)

df %>%
unite(temp, Name, eval, test) %>%
spread(key=type, value=score)

编辑以显示 akrun 代码所处理的原始表格来自:

|Name |eval   |test |type      | score|
|:----|:------|:----|:---------|-----:|
|John |first |1 |pretest | 10|
|John |first |1 |posttest | 15|
|John |first |2 |pretest | 20|
|John |first |2 |postttest | 30|
|John |second |1 |pretest | 35|
|John |second |1 |posttest | 50|
|John |second |2 |pretest | 5|
|John |second |2 |postttest | 10|
|Jane |first |1 |pretest | 40|
|Jane |first |1 |posttest | 20|
|Jane |first |2 |pretest | 10|
|Jane |first |2 |postttest | 20|

最佳答案

我们可以替换“type”列中的多个“t”以使其相同,然后使用 unite 指定 remove = FALSE 来保留初始列并传播

library(dplyr)
library(tidyr)
library(stringr)
df %>%
mutate(type = str_replace(type, "t{2,}", "t")) %>%
unite(new_name, Name, eval, test, remove = FALSE) %>%
spread(type, score)
# new_name Name eval test postest pretest
#1 Jane_first_1 Jane first 1 20 40
#2 Jane_first_2 Jane first 2 20 10
#3 John_first_1 John first 1 15 10
#4 John_first_2 John first 2 30 20
#5 John_second_1 John second 1 50 35
#6 John_second_2 John second 2 10 5

在新版本tidyr_1.0.0中,引入了pivot_wider,它可以用作spread的更通用版本(将是将来已弃用)。因此,不要使用最后的 spread 行,而是使用

 ...%>%
pivot_wider(names_from = type, values_from = score)

关于r - 使用 tidyverse 有条件地转置选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58141103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com