我有一个数据集,其中有一些缺失的值,我想用同一组的其他成员来填充。但是,在某些情况下,每个组有多个值,在这些情况下,我希望每个组中的所有行都被复制为一行包含每个值。
样本数据:
ID group value
1 1 A blue
2 2 A <NA>
3 3 A <NA>
4 4 B green
5 4 B red
6 5 B <NA>
7 6 B <NA>
8 7 C blue
9 8 C green
10 9 C NA
我想结束的是:
ID group value
1 1 A blue
2 2 A blue
3 3 A blue
4 4 B green
5 4 B red
6 5 B green
7 5 B red
8 6 B green
9 6 B red
10 7 C blue
11 7 C green
12 8 C blue
13 8 C green
14 9 C blue
15 9 C green
在某些情况下,该组包含一个具有两个值的ID(例如B组),而其他情况是该组中有多个ID,每个ID都具有不同的值(例如C)。无论如何,我想要一个表,其中该组的每个成员都具有该组中的每个值。我已经找到了一些答案,可以解决A组等简单情况,但每个组都没有一个以上的价值。
====编辑====
我的实际数据集要大得多,这引起了一些其他问题。更新后的示例表如下:
ID group value specific_value dataversion
1 A blue sky_blue version1
2 A <NA> <NA> version2
3 A <NA> <NA> version1
4 B green forest_green version1
4 B red scarlet version1
5 B <NA> <NA> version2
6 B <NA> <NA> <NA>
7 C blue royal_blue version2
8 C green lime_green version1
9 C <NA> <NA> version1
对于每个组,我要为该组的每个成员都包含一行,其中包含该组的每组值+ specific_value(但我不希望一行包含例如blue和lime_green)。我希望其他所有列(ID,组和数据版本)的所有值都保持原样(包括例如,如果数据版本为NA)。
预期输出:
ID group value specific_value dataversion
1 A blue sky_blue version1
2 A blue sky_blue version2
3 A blue sky_blue version1
4 B green forest_green version1
4 B red scarlet version1
5 B green forest_green version2
5 B red scarlet version2
6 B green forest_green <NA>
6 B red scarlet <NA>
7 C blue royal_blue version2
7 C green lime_green version2
8 C blue royal_blue version1
8 C green lime_green version1
9 C blue royal_blue version1
9 C green lime_green version1
即。表中ID,组和数据版本的每种组合都与原始表相同,但是每个组的value和specific_value的每种组合现在都有一行。请注意,在我的实际表中,我有〜50列数据(1个分组列,〜6相当于这里的值/特定值,其余的都像ID / dataversion一样对待),所以我宁愿不必键入每一列名称。
答案 0 :(得分:2)
我们可能在这里需要complete
。按“组”分组后,使用complete
获取每个“组”和“ ID”的unique
非NA“值”的组合
library(dplyr)
library(tidyr)
library(stringr)
df1 %>%
group_by(group) %>%
complete(ID, value = unique(value[!is.na(value)])) %>%
na.omit %>%
select(names(df1))
# A tibble: 15 x 3
# Groups: group [3]
# ID group value
# <int> <chr> <chr>
# 1 1 A blue
# 2 2 A blue
# 3 3 A blue
# 4 4 B green
# 5 4 B red
# 6 5 B green
# 7 5 B red
# 8 6 B green
# 9 6 B red
#10 7 C blue
#11 7 C green
#12 8 C blue
#13 8 C green
#14 9 C blue
#15 9 C green
有了新的数据集,我们可以做到
df2 %>%
group_by(group) %>%
mutate(valnew = str_c(value, specific_value, sep=":")) %>%
select(-value, -specific_value, -dataversion) %>%
complete(ID, valnew = unique(valnew[!is.na(valnew)])) %>%
filter(!is.na(valnew)) %>%
separate(valnew, into = c('value', 'specific_value'), sep=":") %>%
mutate(rn = row_number()) %>%
left_join(df2 %>%
select(ID, dataversion)) %>%
filter(!duplicated(rn)) %>%
select(names(df2))
# A tibble: 15 x 5
# Groups: group [3]
# ID group value specific_value dataversion
# <int> <chr> <chr> <chr> <chr>
# 1 1 A blue sky_blue version1
# 2 2 A blue sky_blue version2
# 3 3 A blue sky_blue version1
# 4 4 B green forest_green version1
# 5 4 B red scarlet version1
# 6 5 B green forest_green version2
# 7 5 B red scarlet version2
# 8 6 B green forest_green <NA>
# 9 6 B red scarlet <NA>
#10 7 C blue royal_blue version2
#11 7 C green lime_green version2
#12 8 C blue royal_blue version1
#13 8 C green lime_green version1
#14 9 C blue royal_blue version1
#15 9 C green lime_green version1
df1 <- structure(list(ID = c(1L, 2L, 3L, 4L, 4L, 5L, 6L, 7L, 8L, 9L),
group = c("A", "A", "A", "B", "B", "B", "B", "C", "C", "C"
), value = c("blue", NA, NA, "green", "red", NA, NA, "blue",
"green", NA)), row.names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10"), class = "data.frame")
df2 <- structure(list(ID = c(1L, 2L, 3L, 4L, 4L, 5L, 6L, 7L, 8L, 9L),
group = c("A", "A", "A", "B", "B", "B", "B", "C", "C", "C"
), value = c("blue", NA, NA, "green", "red", NA, NA, "blue",
"green", NA), specific_value = c("sky_blue", NA, NA, "forest_green",
"scarlet", NA, NA, "royal_blue", "lime_green", NA), dataversion = c("version1",
"version2", "version1", "version1", "version1", "version2",
NA, "version2", "version1", "version1")), class = "data.frame",
row.names = c(NA,
-10L))