我如何访问数据框中的多个元素(列表)
How do i access multple element (list) in a dataframe
我有一个示例数据框,如下所示。
pr_id
product
name
id_234
onion,bean
chris
id_34d
apple
tom
id_87t
plantain, potato, apple
tex
我想访问产品列并创建一个新列,如果苹果在列表中则分配 1,否则分配 0。
所以我希望得到这样的结果:
pr_id
product
name
result
id_234
onion,bean
chris
0
id_34d
apple
tom
1
id_87t
plantain, potato, apple
tex
1
我想到了这样的事情:
my_df$result <- ifelse(my_df$product == 'apple', 1,0)
但这仅适用于第 1 行和第 2 行,但不适用于具有多个元素的最后一行。
请问我该如何处理?
您可以使用 agrepl
搜索字符串中的近似匹配项。如果您使用 ==
,您正在搜索完全匹配。
my_df <-
structure(
list(
pr_id = c("id_234", "id_34d", "id_87t"),
product = c("onion,bean",
"apple", "plantain, potato, apple"),
name = c("chris", "tom",
"tex")
),
class = "data.frame",
row.names = c(NA, -3L)
)
my_df$result <- ifelse(agrepl('apple', my_df$product), 1,0)
或者 tidyverse 方法
library(dplyr)
my_df <-
my_df %>%
mutate(result = as.numeric(agrepl('apple', product)))
my_df
#> pr_id product name result
#> 1 id_234 onion,bean chris 0
#> 2 id_34d apple tom 1
#> 3 id_87t plantain, potato, apple tex 1
使用 dplyr,数据框来自 p。帕乔雷蒂
感谢 AnilGoyal stringr::str_detect
# construct the dataframe
pr_id = c("id_234", "id_34d", "id_87t")
product = c("onion,bean",
"apple", "plantain, potato, apple")
name = c("chris", "tom","tex")
my_df <- data.frame(pr_id, product, name)
# check with case_when and str_detect if apple is in product
my_df <- my_df %>%
mutate(result = case_when(stringr::str_detect(product, "apple") ~ 1,
TRUE ~ 0)
)
我会在 stringr(tidyverse 选项)中使用 str_detect 选项。
my_df <- my_df %>%
mutate(result = ifelse(str_detect(product, "apple"), 1, 0))
使用str_count
library(dplyr)
library(stringr)
df %>%
mutate(result = str_count(product, 'apple'))
我有一个示例数据框,如下所示。
pr_id | product | name |
---|---|---|
id_234 | onion,bean | chris |
id_34d | apple | tom |
id_87t | plantain, potato, apple | tex |
我想访问产品列并创建一个新列,如果苹果在列表中则分配 1,否则分配 0。
所以我希望得到这样的结果:
pr_id | product | name | result |
---|---|---|---|
id_234 | onion,bean | chris | 0 |
id_34d | apple | tom | 1 |
id_87t | plantain, potato, apple | tex | 1 |
我想到了这样的事情:
my_df$result <- ifelse(my_df$product == 'apple', 1,0)
但这仅适用于第 1 行和第 2 行,但不适用于具有多个元素的最后一行。
请问我该如何处理?
您可以使用 agrepl
搜索字符串中的近似匹配项。如果您使用 ==
,您正在搜索完全匹配。
my_df <-
structure(
list(
pr_id = c("id_234", "id_34d", "id_87t"),
product = c("onion,bean",
"apple", "plantain, potato, apple"),
name = c("chris", "tom",
"tex")
),
class = "data.frame",
row.names = c(NA, -3L)
)
my_df$result <- ifelse(agrepl('apple', my_df$product), 1,0)
或者 tidyverse 方法
library(dplyr)
my_df <-
my_df %>%
mutate(result = as.numeric(agrepl('apple', product)))
my_df
#> pr_id product name result
#> 1 id_234 onion,bean chris 0
#> 2 id_34d apple tom 1
#> 3 id_87t plantain, potato, apple tex 1
使用 dplyr,数据框来自 p。帕乔雷蒂
感谢 AnilGoyal stringr::str_detect
# construct the dataframe
pr_id = c("id_234", "id_34d", "id_87t")
product = c("onion,bean",
"apple", "plantain, potato, apple")
name = c("chris", "tom","tex")
my_df <- data.frame(pr_id, product, name)
# check with case_when and str_detect if apple is in product
my_df <- my_df %>%
mutate(result = case_when(stringr::str_detect(product, "apple") ~ 1,
TRUE ~ 0)
)
我会在 stringr(tidyverse 选项)中使用 str_detect 选项。
my_df <- my_df %>%
mutate(result = ifelse(str_detect(product, "apple"), 1, 0))
使用str_count
library(dplyr)
library(stringr)
df %>%
mutate(result = str_count(product, 'apple'))