I have a list listDF of 30 data frames (~1000 rows X 3 columns). In the last column of each one, I have a composite character as shown below :
Date Origin Chemical
28/10/2012 Artificial nuclides Cs-137__Sea
28/10/2012 Natural nuclides Ra-226__Clouds
28/10/2012 Natural nuclides Ra-228__Sands
28/10/2012 Natural nuclides Th-228__Sea
28/10/2012 Artificial nuclides Cs-137__Rocks
For the last column of each df, how can I simply remove "__Sea", "__Clouds"... and just keep the chemical name ?
CodePudding user response:
Base R solution:
## requires R version 4.1 or higher
listDF = lapply(listDF,
\(df) {
df[["Chemical"]] = sub(pattern = "__.*", replacement = "", df[["Chemical"]])
df
})
## any R version
listDF = lapply(listDF,
function(df) {
df[["Chemical"]] = sub(pattern = "__.*", replacement = "", df[["Chemical"]])
df
})
CodePudding user response:
For-loop solution:
for (i in 1:nrow(listDF)){
listDF[i, "Chemical"] = unlist(strsplit(listDF[i, "Chemical"], "__"))[1]
}
