首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用我的ifelse语句无法正确拉取字符串

使用我的ifelse语句无法正确拉取字符串
EN

Stack Overflow用户
提问于 2021-06-17 07:57:37
回答 1查看 54关注 0票数 0

我尝试在dataframe中基于与其他列匹配的部分字符串创建一个新列,基本上与这里所做的完全相同:Create new column in dataframe based on partial string matching other column。但是,我的字符串显然不能正确匹配。

向您展示我是如何构建数据帧的,以防这会导致我的问题

代码语言:javascript
运行
复制
library(dplyr)
#Load data file

LIT_raw<-read.csv("BIASK_Terr_Data Extraction_Master JJT_EBCleaning-2021-06-10.csv", header = T, stringsAsFactors = F) 

#check import

head(LIT_raw)
names(LIT_raw)

##make my dataframe
#subset for "Focus", which is whether study was research and monitoring or management and then other relevant columns

LITsub <- LIT_raw[58:135, (names(LIT_raw) %in% c("Year", "Citation", "Focus", "StudyArchetype1", "StudyArchetype2", "Design_IK_WS_roles1", "Design_IK_WS_roles2", "Implementa_IK_roles1", "Implementa_IK_roles2", "Implementa_WS_roles1", "Implementa_WS_roles2", "Analysis_IK_WS_roles1", "Analysis_IK_WS_roles2", "RepDesc_IK_WS_roles1", "RepDesc_IK_WS_roles2"))]

length(LITsub)
names(LITsub)
LITsub$Focus


#[1] "Year"                  "Citation"              "Focus"                 "Design_IK_WS_roles1"  
#[5] "Design_IK_WS_roles2"   "Implementa_IK_roles1"  "Implementa_IK_roles2"  "Implementa_WS_roles1" 
#[9] "Implementa_WS_roles2"  "Analysis_IK_WS_roles1" "Analysis_IK_WS_roles2" "RepDesc_IK_WS_roles1" 
#[13] "RepDesc_IK_WS_roles2"  "StudyArchetype1"       "StudyArchetype2"

创建名为Design_IK_WS_comb的新列(并在Design_IK_WS_roles2之后插入此新列),将Design_IK_WS_roles1 Design_IK_WS_roles2与条目之间的逗号组合在一起,并将结果放入Design_IK_WS_comb中

代码语言:javascript
运行
复制
LITsub$Design_IK_WS_comb = paste(LITsub$Design_IK_WS_roles1, LITsub$Design_IK_WS_roles2, sep=",")

创建名为Design_ifhowbraid #的新列#如果列Design_IK_WS_comb包含"IK和WS用于评估相同的事物,IK和WS用于评估不同的事物或没有编织实践-仅参与分析“,则将这些短语取出并放入Design_ifhowbraid #如果列Design_IK_WS_comb不包含"IK和WS用于评估相同的事物,IK和WS用于评估不同的事物或没有编织实践-仅IK或WS从事分析”中的任何一项,则在Design_ifhowbraid中包括短语“手动编写代码”

代码语言:javascript
运行
复制
LITsub$Design_ifhowbraid <- ifelse(grepl("IK and WS used to assess the same things", LITsub$Design_IK_WS_comb, ignore.case = T), "IK and WS used to assess the same things",
  ifelse(grepl("IK and WS used to assess different things", LITsub$Design_IK_WS_comb, ignore.case = T), "IK and WS used to assess different things",
  ifelse(grepl("No weaving practices - only IK or WS engaged in analysis", LITsub$Design_IK_WS_comb, ignore.case = T), "No weaving practices - only IK or WS engaged in analysis", "CodeManually")))

#The code completes, but then when I go in to check

LITsub$Design_ifhowbraid

即使Design_IK_WS_comb中有完全匹配的内容,所有行都显示为"CodeManually“。例如,如果我执行LITsub$Design_IK_WS_comb,其中一行/结果是#65 "Multiple,Use IK as local scale expertise,IK用于制定研究问题和假设,西方科学告知IK方法,IK和WS用于不同的事情“。

代码语言:javascript
运行
复制
dput(LITsub)

输出:

代码语言:javascript
运行
复制
structure(list(Year = c(2017L, 2017L, 2018L, 2014L, 2016L, 2018L, 
2009L, 2020L, 2008L, 2007L, 2019L, 2017L, 2016L, 1994L, 2021L, 
2018L, 2008L, 2007L, 2011L, 2008L, 2009L, 2011L, 2017L, 1992L, 
2003L, 2019L, 2018L, 2019L, 2006L, 2012L, 2010L, 2003L, 2018L, 
2016L, 2014L, 2016L, 2019L, 2018L, 2004L, 1996L, 2020L, 2018L, 
2017L, 2016L, 2016L, 2000L, 2020L, 2020L, 2010L, 2006L, 2018L, 
2005L, 2002L, 2009L, 2016L, 2009L, 2015L, 2019L, 2009L, 2011L, 
2010L, 2018L, 2014L, 2005L, 2019L, 2019L, 2012L, 2014L, 2014L, 
2019L, 2008L, 2008L, 2018L, 2008L, 2012L, 2003L, 2015L, 2016L
), Focus = c("Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring", "Research and monitoring", "Research and monitoring", 
"Research and monitoring"), Design_IK_WS_roles1 = c("Multiple", 
"Multiple", "Multiple", "No weaving practices - only IK or WS informing research design", 
"Multiple", "Western science informing IK methods", "Western science informing IK methods", 
"IK and WS used for the same things", "Multiple", "Use IK as local scale expertise", 
"Multiple", "Multiple", "IK and WS used for the same things", 
"Use IK as local scale expertise", "Multiple", "Multiple", "Multiple", 
"Multiple", "IK and WS used for different things", "IK and WS used for the same things", 
"Use IK as local scale expertise", "Use IK as local scale expertise", 
"Multiple", "Multiple", "Multiple", "IK and WS used for different things", 
"No weaving practices - only IK or WS informing research design", 
"Multiple", "IK and WS used for the same things", "Multiple", 
"Use IK as local scale expertise", "Multiple", "Multiple", "Multiple", 
"IK used in formulating research questions and hypotheses", "Use IK as local scale expertise", 
"Multiple", "Multiple", "Multiple", "Multiple", "Multiple", "Multiple", 
"Multiple", "IK as a source of historical/ baseline information", 
"Multiple", "IK used in formulating research questions and hypotheses", 
"Multiple", "Multiple", "Multiple", "Multiple", "Multiple", "Multiple", 
"Multiple", "IK used in formulating research questions and hypotheses", 
"Multiple", "Multiple", "Multiple", "Multiple", "Multiple", "Multiple", 
"Multiple", "Multiple", "Multiple", "Multiple", "Use IK as local scale expertise", 
"Multiple", "Multiple", "Multiple", "Multiple", "Use IK as local scale expertise", 
"Use IK as local scale expertise", "Use IK as local scale expertise", 
"Multiple", "Multiple", "No weaving practices - only IK or WS informing research design", 
"IK used in formulating research questions and hypotheses", "Multiple", 
"Use IK as local scale expertise"), Design_IK_WS_roles2 = c("IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/baseline information", 
"Use IK as local scale expertise, IK as a source of historical/baseline information, IK used in formulating research questions and hypotheses", 
"IK and WS used for different things, IK as a source of historical/baseline information, Western science informing IK methods", 
NA, "IK and WS used for different things, Use IK as local scale expertise, IK used in formulating research questions and hypotheses", 
NA, NA, NA, "IK and WS used for different things, Use IK as local scale expertise, IK used in formulating research questions and hypotheses", 
NA, "IK and WS used for different things, Use IK as local scale expertise", 
"IK and WS used for the same things, IK used in formulating research questions and hypotheses", 
NA, NA, "IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise, IK used in formulating research questions and hypotheses", 
"IK and WS used for the same things, IK as a source of historical/baseline information, IK used in formulating research questions and hypotheses", 
"IK and WS used for the same things, IK and WS used for different things, IK used in formulating research questions and hypotheses", 
"IK and WS used for different things, Use IK as local scale expertise", 
NA, NA, NA, NA, "IK and WS used for different things, Use IK as local scale expertise", 
"IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/ baseline information", 
NA, NA, "Use IK as local scale expertise, IK as a source of historical/ baseline information", 
NA, "IK and WS used for different things, IK used in formulating research questions and hypotheses", 
NA, "Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information, Western science informing IK methods", 
"IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/baseline information, IK used in formulating research questions and hypotheses", 
NA, NA, "IK as a source of historical/ baseline information, Use IK as local scale expertise", 
"IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses, other: IK informing WS methods", 
"Western science informing IK methods, IK as a source of historical/ baseline information, IK and WS used for different things", 
"Use IK as local scale expertise, Western science informing IK methods", 
"Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information, Western science informing IK methods", 
"IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
NA, "IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
NA, "Use IK as local scale expertise, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"Multiple: IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"Multiple: IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"Multiple: IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information, Western science informing IK methods", 
"Multiple: IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information, Western science informing IK methods", 
"IK and WS used for the same things, IK as a source of historical/ baseline information", 
"IK and WS used for the same things, IK and WS used for different things, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
NA, "IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise", 
"IK and WS used for the same things, IK and WS used for different things", 
"IK and WS used for the same things, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise", 
"IK and WS used for different things, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"Use IK as local scale expertise, IK and WS used for different things", 
"IK and WS used for the same things, IK used in formulating research questions and hypotheses, Western science informing IK methods", 
"IK used in formulating research questions and hypotheses, IK and WS used for different things, Use IK as local scale expertise, ", 
"Use IK as local scale expertise, Western science informing IK methods", 
NA, "Use IK as local scale expertise, IK used in formulating research questions and hypotheses, Western science informing IK methods, IK and WS used for different things ", 
"IK and WS used for the same things, Use IK as local scale expertise", 
"Use IK as local scale expertise, IK and WS used for the same things", 
"Use IK as local scale expertise,\nIK as a source of historical/ baseline information,\nIK used in formulating research questions and hypotheses", 
NA, NA, NA, "Use IK as local scale expertise,\nIK as a source of historical/ baseline information,\nIK used in formulating research questions and hypotheses", 
"Use IK as local scale expertise,\nIK as a source of historical/ baseline information", 
NA, NA, "Use IK as local scale expertise,\nIK as a source of historical/ baseline information,\nIK used in formulating research questions and hypotheses", 
NA),.............., Design_IK_WS_comb = c("Multiple,IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/baseline information", 
"Multiple,Use IK as local scale expertise, IK as a source of historical/baseline information, IK used in formulating research questions and hypotheses", 
"Multiple,IK and WS used for different things, IK as a source of historical/baseline information, Western science informing IK methods", 
"No weaving practices - only IK or WS informing research design,NA", 
"Multiple,IK and WS used for different things, Use IK as local scale expertise, IK used in formulating research questions and hypotheses", 
"Western science informing IK methods,NA", "Western science informing IK methods,NA", 
"IK and WS used for the same things,NA", "Multiple,IK and WS used for different things, Use IK as local scale expertise, IK used in formulating research questions and hypotheses", 
"Use IK as local scale expertise,NA", "Multiple,IK and WS used for different things, Use IK as local scale expertise", 
"Multiple,IK and WS used for the same things, IK used in formulating research questions and hypotheses", 
"IK and WS used for the same things,NA", "Use IK as local scale expertise,NA", 
"Multiple,IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise, IK used in formulating research questions and hypotheses", 
"Multiple,IK and WS used for the same things, IK as a source of historical/baseline information, IK used in formulating research questions and hypotheses", 
"Multiple,IK and WS used for the same things, IK and WS used for different things, IK used in formulating research questions and hypotheses", 
"Multiple,IK and WS used for different things, Use IK as local scale expertise", 
"IK and WS used for different things,NA", "IK and WS used for the same things,NA", 
"Use IK as local scale expertise,NA", "Use IK as local scale expertise,NA", 
"Multiple,IK and WS used for different things, Use IK as local scale expertise", 
"Multiple,IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"Multiple,IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"IK and WS used for different things,NA", "No weaving practices - only IK or WS informing research design,NA", 
"Multiple,Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"IK and WS used for the same things,NA", "Multiple,IK and WS used for different things, IK used in formulating research questions and hypotheses", 
"Use IK as local scale expertise,NA", "Multiple,Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"Multiple,IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information, Western science informing IK methods", 
"Multiple,IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/baseline information, IK used in formulating research questions and hypotheses", 
"IK used in formulating research questions and hypotheses,NA", 
"Use IK as local scale expertise,NA", "Multiple,IK as a source of historical/ baseline information, Use IK as local scale expertise", 
"Multiple,IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses, other: IK informing WS methods", 
"Multiple,Western science informing IK methods, IK as a source of historical/ baseline information, IK and WS used for different things", 
"Multiple,Use IK as local scale expertise, Western science informing IK methods", 
"Multiple,Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"Multiple,IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information, Western science informing IK methods", 
"Multiple,IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"IK as a source of historical/ baseline information,NA", "Multiple,IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"IK used in formulating research questions and hypotheses,NA", 
"Multiple,Use IK as local scale expertise, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"Multiple,Multiple: IK and WS used for the same things, Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"Multiple,Multiple: IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"Multiple,Multiple: IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information, Western science informing IK methods", 
"Multiple,Multiple: IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information, Western science informing IK methods", 
"Multiple,IK and WS used for the same things, IK as a source of historical/ baseline information", 
"Multiple,IK and WS used for the same things, IK and WS used for different things, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"IK used in formulating research questions and hypotheses,NA", 
"Multiple,IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise, IK as a source of historical/ baseline information", 
"Multiple,IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise", 
"Multiple,IK and WS used for the same things, IK and WS used for different things", 
"Multiple,IK and WS used for the same things, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"Multiple,IK and WS used for the same things, IK and WS used for different things, Use IK as local scale expertise", 
"Multiple,IK and WS used for different things, IK as a source of historical/ baseline information, IK used in formulating research questions and hypotheses", 
"Multiple,Use IK as local scale expertise, IK and WS used for different things", 
"Multiple,IK and WS used for the same things, IK used in formulating research questions and hypotheses, Western science informing IK methods", 
"Multiple,IK used in formulating research questions and hypotheses, IK and WS used for different things, Use IK as local scale expertise, ", 
"Multiple,Use IK as local scale expertise, Western science informing IK methods", 
"Use IK as local scale expertise,NA", "Multiple,Use IK as local scale expertise, IK used in formulating research questions and hypotheses, Western science informing IK methods, IK and WS used for different things ", 
"Multiple,IK and WS used for the same things, Use IK as local scale expertise", 
"Multiple,Use IK as local scale expertise, IK and WS used for the same things", 
"Multiple,Use IK as local scale expertise,\nIK as a source of historical/ baseline information,\nIK used in formulating research questions and hypotheses", 
"Use IK as local scale expertise,NA", "Use IK as local scale expertise,NA", 
"Use IK as local scale expertise,NA", "Multiple,Use IK as local scale expertise,\nIK as a source of historical/ baseline information,\nIK used in formulating research questions and hypotheses", 
"Multiple,Use IK as local scale expertise,\nIK as a source of historical/ baseline information", 
"No weaving practices - only IK or WS informing research design,NA", 
"IK used in formulating research questions and hypotheses,NA", 
"Multiple,Use IK as local scale expertise,\nIK as a source of historical/ baseline information,\nIK used in formulating research questions and hypotheses", 
"Use IK as local scale expertise,NA"), Design_ifhowbraid = c("CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually", "CodeManually", "CodeManually", "CodeManually", 
"CodeManually")), row.names = 57:134, class = "data.frame")

另外,如果我想用所有这些新列重命名新添加到dataframe中的数据,我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-18 13:14:22

我认为您正在检查与数据中存在的文本不同的文本。试试这个-

代码语言:javascript
运行
复制
ifelse(grepl("IK and WS used for the same things", 
       LITsub$Design_IK_WS_comb, ignore.case = T), 
       "IK and WS used to assess the same things",
       ifelse(grepl("IK and WS used for different things", 
       LITsub$Design_IK_WS_comb, ignore.case = T), 
       "IK and WS used to assess different things",
       ifelse(grepl("No weaving practices - only IK or WS engaged in analysis", 
       LITsub$Design_IK_WS_comb, ignore.case = T), 
       "No weaving practices - only IK or WS engaged in analysis", 
       "CodeManually")))

另外,看看?case_when,它可能会帮助您避免这种嵌套的ifelse调用,并使代码更易于阅读。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68011304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档