我有一个数据框...
df <- tibble(
id = 1:10,
family = c("a","a","b","b","c", "d", "e", "f", "g", "h"),
col1_a = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
col1_b = c(1, 2, 3, 4, NA, NA, NA, NA, NA, NA),
col2_a = c(11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
col2_b = c(11, 12, 13, 14, NA, NA, NA, NA, NA, NA),
)
家庭最多只能包含 2 个成员(因此他们是个人或成对)。
对于个人(只有一行的家庭,即 id = 5:10),我想将 50% 的数据从以“a”结尾的列随机移动到以“b”结尾的列。
最后,数据应如下所示(取决于使用了 50% 的行)...
df <- tibble(
id = 1:10,
family = c("a","a","b","b","c", "d", "e", "f", "g", "h"),
col1_a = c(1, 2, 3, 4, 5, NA, 7, NA, 9, NA),
col1_b = c(1, 2, 3, 4, NA, 6, NA, 8, NA, 10),
col2_a = c(11, 12, 13, 14, NA, NA, 17, 18, NA, 20),
col2_b = c(11, 12, 13, 14, 15, 16, NA, NA, 19, NA),
)
我希望能够通过 group_by 和 mutate 的组合来做到这一点,因为我主要使用 Tidyverse。
更新:我忘了提到以“a”结尾的列中的值如果移至“b”,则应替换为 NA。