我有一个数据集,500人随机回答275个问题中的5个问题,范围为1-5。
library(dplyr)
set.seed(13)
df <- tibble(id = rep(1:500, 5),
q = sample.int(n = 275, size = max(id)*5, replace = T),
ans = sample.int(n = 5, size = max(id)*5, replace = T))
我的任务是为每个人随机选择5个答案中的一个(其他人也回答了(,并将其与随机选择的回答相同问题的其他人进行核对。如果两个回答相同,我会标记为真,如果不相同,我将标记为假。
我想通过根据不止一个人回答的问题为每个人分配一个选择的问题来解决这个问题:
sampled_q <-
df %>%
group_by(q) %>%
mutate(n_times_answer = n()) %>%
filter(n_times_answer >= 2) %>%
group_by(id) %>%
sample_n(1) %>%
transmute(id, q, assigned = T)
df %>%
left_join(sampled_q)
但从这里开始,我不知道如何处理支票。这也是低效的,因为一旦我检查了一个人的回复,我就检查了两个回复,所以从技术上讲,我可以标记两个人的T/F,尽管高效对我来说不是很重要
我还考虑过重塑我的数据:
df %>%
pivot_wider(id_cols = id,
names_from = q,
values_from = ans) %>%
unnest(everything())
但我发现这很慢,我也被困在这里了。
如有任何帮助,我们将不胜感激。
从每个回答者中抽取一个有效问题,然后将其连接回df
。
df %>%
group_by(q) %>%
filter(n_distinct(id) > 1) %>% # Keep only questions that have more than one answerer
group_by(id) %>%
sample_n(1) %>% # Keep only one question from each answerer
inner_join(df, by = "q") %>% # Join it back on the main table to identify other answers to the same question
filter(id.x != id.y) %>% # Don't include answers from the same answerer
group_by(id.x) %>%
sample_n(1) %>% # Keep only one other answer
mutate(matched = ans.x == ans.y) # Check if the answers matched
#> # A tibble: 500 x 6
#> # Groups: id.x [500]
#> id.x q ans.x id.y ans.y matched
#> <int> <int> <int> <int> <int> <lgl>
#> 1 1 175 3 106 3 TRUE
#> 2 2 15 5 117 4 FALSE
#> 3 3 256 4 366 3 FALSE
#> 4 4 268 4 194 4 TRUE
#> 5 5 161 3 485 5 FALSE
#> 6 6 100 1 390 4 FALSE
#> 7 7 248 5 307 2 FALSE
#> 8 8 126 5 341 4 FALSE
#> 9 9 65 2 93 2 TRUE
#> 10 10 48 1 461 5 FALSE
#> # … with 490 more rows