[英]How would I recursively add values in a tibble using purr in the tidyverse
[英]How do I add extra rows to each column across all groups in tibble using tidyverse?
如何为数据框中每个组id
的所有列(分组变量除外)添加额外的行?
数据:
> library(tidyverse)
> df <- tibble(id = c("A", "B", "C"), day = c(3, 1, 2), station = c(10, 9, 2))
> df
# A tibble: 3 × 3
id day station
<chr> <dbl> <dbl>
1 A 3 10
2 B 1 9
3 C 2 2
预计 output:
# A tibble: 6 × 3
# Groups: id [3]
id day station
<chr> <dbl> <dbl>
1 A 2 9
2 A 3 10
3 B 0 8
4 B 1 9
5 C 1 1
6 C 2 2
我可以使用以下方式更改day
:
> df %>% group_by(id) %>% complete(day = (day - 1):day)
# A tibble: 6 × 3
# Groups: id [3]
id day station
<chr> <dbl> <dbl>
1 A 2 NA
2 A 3 10
3 B 0 NA
4 B 1 9
5 C 1 NA
6 C 2 2
但是我不能适当地使用mutate
来为station
做这个,因为我不知道如何正确地引用里面的每一列complete
失败的尝试:
> df %>%
+ group_by(id) %>%
+ mutate(across(c("day", "station"), complete((.x - 1):.x)))
简化 ThomasIsCoding 的回答:
df %>%
group_by(id) %>%
summarize(across(day:station, ~ .x - 1:0))
如果您只想增加值并且没有为每个组指定特定范围,我认为在这里使用complete()
没有任何优势。
这适用于您想要的 output:
df %>%
bind_rows(df %>% mutate(across(c('day', 'station'), ~.x - 1 ))) %>%
arrange(id)
# A tibble: 6 × 3
# id day station
# <chr> <dbl> <dbl>
# 1 A 3 10
# 2 A 2 9
# 3 B 1 9
# 4 B 0 8
# 5 C 2 2
# 6 C 1 1
在这里,我附加了相同的 dataframe 但带有变异的列,因此:
df %>% mutate(across(c('day', 'station'), ~.x - 1 ))
是:
# A tibble: 3 × 3
id day station
<chr> <dbl> <dbl>
1 A 2 9
2 B 0 8
3 C 1 1
然后使用bind_rows
,我将这些行附加到原始数据框,这带来了:
# A tibble: 6 × 3
id day station
<chr> <dbl> <dbl>
1 A 3 10
2 B 1 9
3 C 2 2
4 A 2 9
5 B 0 8
6 C 1 1
最后我只是按id
排列,所以行看起来像你的例子。
data.table
选项简化了问题
> library(data.table)
> setDT(df)[, lapply(.SD, `-`, 1:0), id]
id day station
1: A 2 9
2: A 3 10
3: B 0 8
4: B 1 9
5: C 1 1
6: C 2 2
或者,使用list
+ unnest
library(tidyr)
df %>%
group_by(id) %>%
mutate(across(day:station, ~ list(.x - (1:0)))) %>%
unnest(day:station) %>%
ungroup()
这使
# A tibble: 6 × 3
id day station
<chr> <dbl> <dbl>
1 A 2 9
2 A 3 10
3 B 0 8
4 B 1 9
5 C 1 1
6 C 2 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.