我有以下玩具藤壶:

df <- data.frame(
  product = c("apple", "banana", "cherry", "durian", "eggplant", "fuyu"),
  ingredients = c("flour|fibre|500", "sugar|500", "505|wheat|flavouring", "fibre(500)|eggs", "wholegrainrice|sesameoil", "500|fibre|500"),
  stringsAsFactors = FALSE
)

我的目标是检测纤维是否出现在产品成分中,计算纤维出现的次数,并提取用于记录产品成分中纤维的值.

出于本分析的目的,纤维可以在产品成分中表示为"纤维"、"500"或"纤维(500)".

我当前的代码是:

library(tidyverse)

fibre_strings_to_check <- c("fibre", "500", "fibre\\(500\\)")

df2 <- df %>%
  mutate(
    fibre_present = str_detect(ingredients, paste(fibre_strings_to_check, collapse = "|")),
    fibre_count = str_count(ingredients, paste(fibre_strings_to_check, collapse = "|")),
    fibre_used = str_extract_all(ingredients, paste(fibre_strings_to_check, collapse = "|"))
  )

它给出df2的输出为:

| product  | ingredients                 | fibre_present | fibre_count | fibre_used        |
|----------|-----------------------------|---------------|-------------|-------------------|
| apple    | flour\|fibre\|500           | TRUE          | 2           | fibre, 500        |
| banana   | sugar\|500                  | TRUE          | 1           | 500               |
| cherry   | 505\|wheat\|flavouring      | FALSE         | 0           |                   |
| durian   | fibre(500)\|eggs            | TRUE          | 2           | fibre, 500        |
| eggplant | wholegrainrice\|sesameoil   | FALSE         | 0           |                   |
| fuyu     | 500\|fibre\|500             | TRUE          | 3           | 500, fibre, 500   |

我遇到的问题是"榴莲"产品.我希望将"Fibre(500)"算作Fibre的一个值/实例,正如fibre_strings_to_check中定义的那样.但由于它似乎与fibre_strings_to_check中的其他纤维实例相匹配,因此它算作纤维的两个值/实例.

我的预期输出df2是:

| product  | ingredients                 | fibre_present | fibre_count | fibre_used        |
|----------|-----------------------------|---------------|-------------|-------------------|
| apple    | flour\|fibre\|500           | TRUE          | 2           | fibre, 500        |
| banana   | sugar\|500                  | TRUE          | 1           | 500               |
| cherry   | 505\|wheat\|flavouring      | FALSE         | 0           |                   |
| durian   | fibre(500)\|eggs            | TRUE          | 1           | fibre(500)        |
| eggplant | wholegrainrice\|sesameoil   | FALSE         | 0           |                   |
| fuyu     | 500\|fibre\|500             | TRUE          | 3           | 500, fibre, 500   |

How do I adjust the script so that there is no double counting of what is intended to be a single value?

推荐答案

一个快速解决方案是重新排列载体fibre_strings_to_check ,以便"fibre\\(500\\)"比其余值先出现.

library(dplyr)
library(stringr)

fibre_strings_to_check <- c("fibre\\(500\\)", "fibre", "500")
fibre_regex <- paste(fibre_strings_to_check, collapse = "|")

df2 <- df %>%
  mutate(
    fibre_present = str_detect(ingredients, fibre_regex),
    fibre_count = str_count(ingredients, fibre_regex),
    fibre_used = str_extract_all(ingredients, fibre_regex)
  )

df2
#   product              ingredients fibre_present fibre_count      fibre_used
#1    apple          flour|fibre|500          TRUE           2      fibre, 500
#2   banana                sugar|500          TRUE           1             500
#3   cherry     505|wheat|flavouring         FALSE           0                
#4   durian          fibre(500)|eggs          TRUE           1      fibre(500)
#5 eggplant wholegrainrice|sesameoil         FALSE           0                
#6     fuyu            500|fibre|500          TRUE           3 500, fibre, 500

R相关问答推荐

DT::可数据的正规表达OR运算符问题

在通过最大似然估计将ODE模型与数据匹配时,为什么要匹配实际参数的转换值?

R形式的一维数字线/箱形图样式图表

在ggplot Likert条中添加水平线

使用tidyverse方法绑定行并从一组管道列表执行左连接

gt()从gt为相同内容的单元格 colored颜色 不同?

R Sapply函数产生的值似乎与for循环方法略有不同

如何得到R中唯一的组合群?

条形图和在Ploly中悬停的问题

非线性混合效应模型(NLME)预测变量的置信区间

如何从R ggplot图片中获取SVG字符串?

从服务器在Shiny中一起渲染图标和文本

有没有可能用shiny 的书签恢复手风琴面板?

如何对r中包含特定(未知)文本的行求和?

Geom_arcbar()中出错:找不到函数";geom_arcbar";

有没有办法一次粘贴所有列

如何获取R chromote中的当前URL?

在GT()中的列之间添加空格

分隔日期格式为2020年7月1日

如何计算多个变量的百分比与总和的百分比?