Dataframe 列循环和基于 R 中条件的字符串连接(pref dplyr)
Dataframe column looping and string concatenation based on conditional in R (pref dplyr)
我有一个 2 列数据框。第一列包含 class 项的单个条目(在本例中为蔬菜)。第二列是进货 new_item
,它们是不同类别的杂货(肉类、水果、蔬菜等)。
library(tidyverse)
current <- tibble::tribble(
~prev_veg, ~new_item,
"cabbage", "lettuce",
NA, "apple",
NA, "beef",
NA, "spinach",
NA, "broccoli",
NA, "mango"
)
current
我想遍历新的项目列,只将蔬菜添加到 prev_veg
。任何新的蔬菜项目都需要添加到现有列表中。重要的是,我有一个包含所有可能出现在该列表中的蔬菜的向量。所需的数据框如下。
target_veg <- c("cabbage","lettuce", "spinach", "broccoli"
desired <- tibble::tribble(
~prev_veg, ~new_item,
"cabbage", "lettuce",
"cabbage, lettuce", "apple",
"cabbage, lettuce", "strawbery",
"cabbage, lettuce", "spinach",
"cabbage, lettuce, spinach", "broccoli",
"cabbage, lettuce, spinach, broccoli", "mango"
)
desired
最后,这个数据框中还有多个其他数据列我没有包括在这里(只包括相关列)。请理想地寻找 dplyr 解决方案。
current <- tibble::tribble(
~prev_veg, ~new_item,
"cabbage", "lettuce",
NA, "apple",
NA, "beef",
NA, "spinach",
NA, "broccoli",
NA, "mango"
)
target_veg <- c("cabbage", "lettuce", "spinach", "broccoli")
library(dplyr, warn.conflicts = FALSE)
library(purrr)
current %>%
mutate(
prev_veg = accumulate(
head(new_item, -1),
~ if_else(.y %in% target_veg, paste(.x, .y, sep = ", "), .x),
.init = prev_veg[1]
)
)
#> # A tibble: 6 × 2
#> prev_veg new_item
#> <chr> <chr>
#> 1 cabbage lettuce
#> 2 cabbage, lettuce apple
#> 3 cabbage, lettuce beef
#> 4 cabbage, lettuce spinach
#> 5 cabbage, lettuce, spinach broccoli
#> 6 cabbage, lettuce, spinach, broccoli mango
由 reprex package (v2.0.1)
于 2022-02-24 创建
这也可以通过使用 match
找到索引然后使用 rowwise
粘贴
来创建
library(dplyr)
library(tidyr)
current %>%
mutate(ind = lag(match(new_item, target_veg))) %>%
fill(ind, .direction = "downup") %>%
rowwise %>%
mutate(ind = toString(target_veg[seq(ind)])) %>%
ungroup %>%
mutate(prev_veg = coalesce(prev_veg, ind), .keep = "unused")
-输出
# A tibble: 6 × 2
prev_veg new_item
<chr> <chr>
1 cabbage lettuce
2 cabbage, lettuce apple
3 cabbage, lettuce beef
4 cabbage, lettuce spinach
5 cabbage, lettuce, spinach broccoli
6 cabbage, lettuce, spinach, broccoli mango
注意:rowwise
可能比@IceCreamToucan 的 accumulate
慢。
我有一个 2 列数据框。第一列包含 class 项的单个条目(在本例中为蔬菜)。第二列是进货 new_item
,它们是不同类别的杂货(肉类、水果、蔬菜等)。
library(tidyverse)
current <- tibble::tribble(
~prev_veg, ~new_item,
"cabbage", "lettuce",
NA, "apple",
NA, "beef",
NA, "spinach",
NA, "broccoli",
NA, "mango"
)
current
我想遍历新的项目列,只将蔬菜添加到 prev_veg
。任何新的蔬菜项目都需要添加到现有列表中。重要的是,我有一个包含所有可能出现在该列表中的蔬菜的向量。所需的数据框如下。
target_veg <- c("cabbage","lettuce", "spinach", "broccoli"
desired <- tibble::tribble(
~prev_veg, ~new_item,
"cabbage", "lettuce",
"cabbage, lettuce", "apple",
"cabbage, lettuce", "strawbery",
"cabbage, lettuce", "spinach",
"cabbage, lettuce, spinach", "broccoli",
"cabbage, lettuce, spinach, broccoli", "mango"
)
desired
最后,这个数据框中还有多个其他数据列我没有包括在这里(只包括相关列)。请理想地寻找 dplyr 解决方案。
current <- tibble::tribble(
~prev_veg, ~new_item,
"cabbage", "lettuce",
NA, "apple",
NA, "beef",
NA, "spinach",
NA, "broccoli",
NA, "mango"
)
target_veg <- c("cabbage", "lettuce", "spinach", "broccoli")
library(dplyr, warn.conflicts = FALSE)
library(purrr)
current %>%
mutate(
prev_veg = accumulate(
head(new_item, -1),
~ if_else(.y %in% target_veg, paste(.x, .y, sep = ", "), .x),
.init = prev_veg[1]
)
)
#> # A tibble: 6 × 2
#> prev_veg new_item
#> <chr> <chr>
#> 1 cabbage lettuce
#> 2 cabbage, lettuce apple
#> 3 cabbage, lettuce beef
#> 4 cabbage, lettuce spinach
#> 5 cabbage, lettuce, spinach broccoli
#> 6 cabbage, lettuce, spinach, broccoli mango
由 reprex package (v2.0.1)
于 2022-02-24 创建这也可以通过使用 match
找到索引然后使用 rowwise
粘贴
library(dplyr)
library(tidyr)
current %>%
mutate(ind = lag(match(new_item, target_veg))) %>%
fill(ind, .direction = "downup") %>%
rowwise %>%
mutate(ind = toString(target_veg[seq(ind)])) %>%
ungroup %>%
mutate(prev_veg = coalesce(prev_veg, ind), .keep = "unused")
-输出
# A tibble: 6 × 2
prev_veg new_item
<chr> <chr>
1 cabbage lettuce
2 cabbage, lettuce apple
3 cabbage, lettuce beef
4 cabbage, lettuce spinach
5 cabbage, lettuce, spinach broccoli
6 cabbage, lettuce, spinach, broccoli mango
注意:rowwise
可能比@IceCreamToucan 的 accumulate
慢。