我有一个数据集,其中有500个人,在1-5个问题的275个问题中随机回答了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个响应中(从其他人也回答的响应中)随机选择一个,然后与随机选择的回答相同问题的其他人进行比较。如果两个响应相同,则将其标记为true,否则将其标记为false。
我考虑过通过在多个人回答以下问题的基础上为每个人分配一个选择的问题来解决这个问题:
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())
但是我发现这很慢,而且我也被困在这里。
任何帮助,将不胜感激。
从每个回答者中抽样1个有效问题,然后将其重新加入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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句