将多个不互斥的虚拟变量转换为单个分类变量,添加新行
Converting multiple dummy variables that are not mutually exclusive into single categorical variable, adding new rows
我有以下数据框:
data <- data.frame(id=c(1, 1, 2, 2),
task=c(1, 2, 1, 2),
strategy1=c("1", "1", "0", "1"),
strategy2=c("0", "0", "1", "1"),
strategy3=c("0", "1", "0", "1"))
我的目标是将不同策略的虚拟变量组合成一个单一的分类变量,'strategy'。如果参与者在一项任务中使用了多种策略,则必须相应地创建具有相同 'id' 和 'task' 数字的新行,因为应该只有一个 'strategy' 变量。
对于给定的示例,数据框最终应如下所示:
data_single <- data.frame(id=c(1, 1, 1, 2, 2, 2, 2),
task=c(1, 2, 2, 1, 2, 2, 2),
strategy=c("1", "1", "3", "2", "1", "2", "3"))
谁能告诉我如何实现这个目标?
library(tidyr)
library(dplyr)
tidyr::pivot_longer(
data,
cols = starts_with("strategy"),
names_prefix = "strategy",
names_to = "strategy"
) %>%
filter(value == 1) %>%
select(-value)
# # A tibble: 7 x 3
# id task strategy
# <dbl> <dbl> <chr>
# 1 1 1 1
# 2 1 2 1
# 3 1 2 3
# 4 2 1 2
# 5 2 2 1
# 6 2 2 2
# 7 2 2 3
我们也可以用data.table
library(data.table)
melt(setDT(data), measure = patterns('^strategy'),
variable.name = 'strategy')[value == 1, .(id, task, strategy)]
我有以下数据框:
data <- data.frame(id=c(1, 1, 2, 2),
task=c(1, 2, 1, 2),
strategy1=c("1", "1", "0", "1"),
strategy2=c("0", "0", "1", "1"),
strategy3=c("0", "1", "0", "1"))
我的目标是将不同策略的虚拟变量组合成一个单一的分类变量,'strategy'。如果参与者在一项任务中使用了多种策略,则必须相应地创建具有相同 'id' 和 'task' 数字的新行,因为应该只有一个 'strategy' 变量。
对于给定的示例,数据框最终应如下所示:
data_single <- data.frame(id=c(1, 1, 1, 2, 2, 2, 2),
task=c(1, 2, 2, 1, 2, 2, 2),
strategy=c("1", "1", "3", "2", "1", "2", "3"))
谁能告诉我如何实现这个目标?
library(tidyr)
library(dplyr)
tidyr::pivot_longer(
data,
cols = starts_with("strategy"),
names_prefix = "strategy",
names_to = "strategy"
) %>%
filter(value == 1) %>%
select(-value)
# # A tibble: 7 x 3
# id task strategy
# <dbl> <dbl> <chr>
# 1 1 1 1
# 2 1 2 1
# 3 1 2 3
# 4 2 1 2
# 5 2 2 1
# 6 2 2 2
# 7 2 2 3
我们也可以用data.table
library(data.table)
melt(setDT(data), measure = patterns('^strategy'),
variable.name = 'strategy')[value == 1, .(id, task, strategy)]