Reputation: 101
I am trying to create a new column in dataframe based on partial string matching other column, basically exactly what was done here: Create new column in dataframe based on partial string matching other column. However, my strings are clearly not matching correctly.
To show you how I built the dataframe in case this is causing my issue
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"
make new column called Design_IK_WS_comb (and insert this new column after Design_IK_WS_roles2), combine Design_IK_WS_roles1 Design_IK_WS_roles2 with commas between entries and put the result in Design_IK_WS_comb
LITsub$Design_IK_WS_comb = paste(LITsub$Design_IK_WS_roles1, LITsub$Design_IK_WS_roles2, sep=",")
make new column called Design_ifhowbraid #If columns Design_IK_WS_comb contains "IK and WS used to assess the same things, IK and WS used to assess different things or No weaving practices - only IK or WS engaged in analysis" take these phrases out and put them in Design_ifhowbraid #if column Design_IK_WS_comb does not contain any of "IK and WS used to assess the same things, IK and WS used to assess different things or No weaving practices - only IK or WS engaged in analysis" then include the phrase "code manually" in Design_ifhowbraid
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
All of the rows say "CodeManually" even though there are exact matches in Design_IK_WS_comb. For example, if I execute LITsub$Design_IK_WS_comb
One of the rows/results that comes up is #[65] "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 ".
dput(LITsub)
output:
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")
Also, if I want to rename the newly added to dataframe with all these new columns, how would I do that?
Upvotes: 0
Views: 61
Reputation: 388982
I think you are checking for different text than what is present in the data. Try this -
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")))
Also look at ?case_when
which might help you to avoid such nested ifelse
calls and make the code easier to read.
Upvotes: 1