使用tidyverse有条件地转置select行
我有一个我正在使用的数据集,我正在尝试使用tidyverse重塑它 发件人: 致:使用tidyverse有条件地转置select行,r,tidyverse,R,Tidyverse,我有一个我正在使用的数据集,我正在尝试使用tidyverse重塑它 发件人: 致: 试着按分组,以便按姓名、评估和测试对每一组进行分组,这样每个组基本上都是针对给定人员的前测试和后测试 还尝试在名称、评估、测试和类型上使用unite。但是如果我在那之后进行一次排列,那么每个唯一的名称最终都是许多列 还尝试使用key=(新的联合名称)和value=value先对Name、eval、test进行unite,然后进行spread,但输出不是我想要的 我知道可以编写一个循环函数来获取每一个其他值并将
- 试着按分组,以便按姓名、评估和测试对每一组进行分组,这样每个组基本上都是针对给定人员的前测试和后测试
- 还尝试在名称、评估、测试和类型上使用unite。但是如果我在那之后进行一次排列,那么每个唯一的名称最终都是许多列
- 还尝试使用key=(新的联合名称)和value=value先对Name、eval、test进行unite,然后进行spread,但输出不是我想要的
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)
我们可以替换“type”列中的多个“t”使其相同,然后使用
unite
指定remove=FALSE
也保留初始列和spread
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\u 1.0.0
中,引入了pivot\u wide
,它可以用作价差的更通用版本(将来可能会被弃用)。因此,使用
...%>%
pivot_wider(names_from = type, values_from = score)
像……这样的怎么样
数据%
分组依据(名称、评估、类型)%>%
突变(num=1:n(),
新名称=str_c(名称,“,”评估,“,”数值))%>%
解组()%>%
dplyr::选择(新名称、类型、分数)%>%
排列(类型、分数)
这将产生:
#一个tible:6 x 3
新名称后测前测
1 Jane_first_120 40
2 Jane_first_2 20 10
3约翰·菲斯特11510
4约翰·菲斯特2 30 20
5约翰第二大学150 35
6约翰·二等兵2 10 5
实际上只是在编辑打字错误,并且刚刚使用spread
方法解决了这个问题。不知道pivot\u wider
方法。谢谢
|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|
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
...%>%
pivot_wider(names_from = type, values_from = score)