gpt4 book ai didi

r - Dataframe 列循环和基于 R 中条件的字符串连接(pref dplyr)

转载 作者:行者123 更新时间:2023-12-01 23:04:14 25 4
gpt4 key购买 nike

我有一个 2 列数据框。第一列包含一类项目(在本例中为蔬菜)的单个条目。第二列是传入的 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 创建于 2022-02-24 (v2.0.1)

关于r - Dataframe 列循环和基于 R 中条件的字符串连接(pref dplyr),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71257290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com