gpt4 book ai didi

r - 检查项目是否在列表中

转载 作者:行者123 更新时间:2023-12-04 03:31:29 24 4
gpt4 key购买 nike

假设,我有一个格式如下的表格:

CowId    Farm    Week1
1 CB c("Staphylococcus aureus", "Escherichia coli")
2 CB No Growth
3 CB NA
4 CB Staphylococcus aureus

如何检查 Staphylococcus aureus 是否是 Week1 列的成员?我试过了

dt.wide %>%
filter(Week1 %in% "Staphylococcus aureus")

但它只捕获 CowId 4,而不捕获 CowId 1。任何反馈都会有所帮助。

dt.wide <- structure(list(CowId = c(1L, 2L, 3L, 4L), 
Farm = structure(c(1L, 1L, 1L, 1L),
.Label = c("CB", "CB", "CB", "CB"),
class = "factor"),
Week1 = list(c("Staphylococcus aureus", "Escherichia coli"), "No Growth", NA, "Staphylococcus aureus")),
row.names = c(NA, -4L),
class = c("tbl_df", "tbl", "data.frame"))

最佳答案

基础 R 方法:

subset(dt.wide, sapply(Week1, function(x) "Staphylococcus aureus" %in% x))

# CowId Farm Week1
# <int> <fct> <list>
#1 1 CB <chr [2]>
#2 4 CB <chr [1]>

关于r - 检查项目是否在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66716202/

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