R:添加新列完成数据集
R: complete a dataset with a new column added
我的数据集如下所示:
(可视化下面的数据集可能有助于您理解问题)
original <- data.frame(
ID = c(rep("John", 4), "Steve"),
A = as.integer(c(rep(3, 4), 1)),
b = c(2, 3, 4, 2, 2),
B = c(rep(4, 4), 2),
detail1 = c("Yes", "Sure", "No", "Yes", "Yes"),
detail2 = c(rep("Unique1", 4), "Unique2")
)
A
、B
和 b
中的值都是整数。变量 b
在这个数据集中是不完整的,它实际上有从 1 到 B
.
的值
我需要添加一个新变量 a
来完成此数据集,完成的数据集将如下所示:
completed1 <- data.frame(
ID = c(rep("John", 12), rep("Steve", 2)),
a = c(rep(1, 4), rep(2, 4), rep(3, 4), rep(1, 2)),
A = c(rep(3, 12), rep(1, 2)),
b = c(rep(1:4, 3), 1, 2),
B = c(rep(4, 12), rep(2, 2)),
detail1 = c("Absence", "Yes", "Sure", "No", "Absence", "Yes", rep("Absence", 7), "Yes"),
detail2 = c(rep("Unique1", 12), rep("Unique2", 2))
)
a
中的值也是整数,a
的值从 1 到 A
的值。 注意 b
嵌套在 a
.
我认为以这种方式完成数据集最相关的函数是tidyr::complete()
和tidyr::expand()
,但它们只能完成现有变量中值的组合,不能添加新列(变量)。
我知道挑战在于有多个位置可以在 detail1
中分配值对应于新添加的值 a
通过嵌套关系,例如完成的数据集也可以是这样的:
completed2 <- data.frame(
ID = c(rep("John", 12), rep("Steve", 2)),
a = c(rep(1, 4), rep(2, 4), rep(3, 4), rep(1, 2)),
A = c(rep(3, 12), rep(1, 2)),
b = c(rep(1:4, 3), 1, 2),
B = c(rep(4, 12), rep(2, 2)),
detail1 = c("Absence", "Yes", rep("Absence", 4), "Sure", "Absence", "Absence", "Yes", "Absence", "No", "Absence", "Yes"),
detail2 = c(rep("Unique1", 12), rep("Unique2", 2))
)
我想按照上面completed1
的逻辑完成数据集:detail1
中的值首先转到a
中的最小值,如果重复[=中的值15=] 存在(例如,b
中的 Yes
在 original
数据集中的 John
下,重复值转到 a
中的下一个值。
可以这样做吗?
我的实际数据集比这个例子有更多的变量,完成的数据集将有超过 700,000 行,所以我更喜欢快速的方法来自动化它。
非常感谢!!!
如果不符合您的目的,请告诉我。
comp_dummy <- original %>%
group_by(ID) %>%
expand(A = A, a = 1:A, B = B, b = 1:B)
dummy <- original %>%
group_by(ID, A,b,B, detail1) %>%
mutate(a = 1:n())
ID A b B detail1 detail2 a
<chr> <int> <dbl> <dbl> <chr> <chr> <int>
1 John 3 2 4 Yes Unique1 1
2 John 3 3 4 Sure Unique1 1
3 John 3 4 4 No Unique1 1
4 John 3 2 4 Yes Unique1 2
5 Steve 1 2 2 Yes Unique2 1
comp_dummy %>%
full_join(dummy, by = c("ID","A","a","B","b")) %>%
group_by(ID) %>%
mutate(detail2 = unique(detail2[!is.na(detail2)]),
detail1 = replace_na(detail1, "Absence"))
ID A a B b detail1 detail2
<chr> <int> <int> <dbl> <dbl> <chr> <chr>
1 John 3 1 4 1 Absence Unique1
2 John 3 1 4 2 Yes Unique1
3 John 3 1 4 3 Sure Unique1
4 John 3 1 4 4 No Unique1
5 John 3 2 4 1 Absence Unique1
6 John 3 2 4 2 Yes Unique1
7 John 3 2 4 3 Absence Unique1
8 John 3 2 4 4 Absence Unique1
9 John 3 3 4 1 Absence Unique1
10 John 3 3 4 2 Absence Unique1
11 John 3 3 4 3 Absence Unique1
12 John 3 3 4 4 Absence Unique1
13 Steve 1 1 2 1 Absence Unique2
14 Steve 1 1 2 2 Yes Unique2
我的数据集如下所示:
(可视化下面的数据集可能有助于您理解问题)
original <- data.frame(
ID = c(rep("John", 4), "Steve"),
A = as.integer(c(rep(3, 4), 1)),
b = c(2, 3, 4, 2, 2),
B = c(rep(4, 4), 2),
detail1 = c("Yes", "Sure", "No", "Yes", "Yes"),
detail2 = c(rep("Unique1", 4), "Unique2")
)
A
、B
和 b
中的值都是整数。变量 b
在这个数据集中是不完整的,它实际上有从 1 到 B
.
我需要添加一个新变量 a
来完成此数据集,完成的数据集将如下所示:
completed1 <- data.frame(
ID = c(rep("John", 12), rep("Steve", 2)),
a = c(rep(1, 4), rep(2, 4), rep(3, 4), rep(1, 2)),
A = c(rep(3, 12), rep(1, 2)),
b = c(rep(1:4, 3), 1, 2),
B = c(rep(4, 12), rep(2, 2)),
detail1 = c("Absence", "Yes", "Sure", "No", "Absence", "Yes", rep("Absence", 7), "Yes"),
detail2 = c(rep("Unique1", 12), rep("Unique2", 2))
)
a
中的值也是整数,a
的值从 1 到 A
的值。 注意 b
嵌套在 a
.
我认为以这种方式完成数据集最相关的函数是tidyr::complete()
和tidyr::expand()
,但它们只能完成现有变量中值的组合,不能添加新列(变量)。
我知道挑战在于有多个位置可以在 detail1
中分配值对应于新添加的值 a
通过嵌套关系,例如完成的数据集也可以是这样的:
completed2 <- data.frame(
ID = c(rep("John", 12), rep("Steve", 2)),
a = c(rep(1, 4), rep(2, 4), rep(3, 4), rep(1, 2)),
A = c(rep(3, 12), rep(1, 2)),
b = c(rep(1:4, 3), 1, 2),
B = c(rep(4, 12), rep(2, 2)),
detail1 = c("Absence", "Yes", rep("Absence", 4), "Sure", "Absence", "Absence", "Yes", "Absence", "No", "Absence", "Yes"),
detail2 = c(rep("Unique1", 12), rep("Unique2", 2))
)
我想按照上面completed1
的逻辑完成数据集:detail1
中的值首先转到a
中的最小值,如果重复[=中的值15=] 存在(例如,b
中的 Yes
在 original
数据集中的 John
下,重复值转到 a
中的下一个值。
可以这样做吗?
我的实际数据集比这个例子有更多的变量,完成的数据集将有超过 700,000 行,所以我更喜欢快速的方法来自动化它。
非常感谢!!!
如果不符合您的目的,请告诉我。
comp_dummy <- original %>%
group_by(ID) %>%
expand(A = A, a = 1:A, B = B, b = 1:B)
dummy <- original %>%
group_by(ID, A,b,B, detail1) %>%
mutate(a = 1:n())
ID A b B detail1 detail2 a
<chr> <int> <dbl> <dbl> <chr> <chr> <int>
1 John 3 2 4 Yes Unique1 1
2 John 3 3 4 Sure Unique1 1
3 John 3 4 4 No Unique1 1
4 John 3 2 4 Yes Unique1 2
5 Steve 1 2 2 Yes Unique2 1
comp_dummy %>%
full_join(dummy, by = c("ID","A","a","B","b")) %>%
group_by(ID) %>%
mutate(detail2 = unique(detail2[!is.na(detail2)]),
detail1 = replace_na(detail1, "Absence"))
ID A a B b detail1 detail2
<chr> <int> <int> <dbl> <dbl> <chr> <chr>
1 John 3 1 4 1 Absence Unique1
2 John 3 1 4 2 Yes Unique1
3 John 3 1 4 3 Sure Unique1
4 John 3 1 4 4 No Unique1
5 John 3 2 4 1 Absence Unique1
6 John 3 2 4 2 Yes Unique1
7 John 3 2 4 3 Absence Unique1
8 John 3 2 4 4 Absence Unique1
9 John 3 3 4 1 Absence Unique1
10 John 3 3 4 2 Absence Unique1
11 John 3 3 4 3 Absence Unique1
12 John 3 3 4 4 Absence Unique1
13 Steve 1 1 2 1 Absence Unique2
14 Steve 1 1 2 2 Yes Unique2