Hi.
I'm trying to delete the second word of all my rows that contain two words. So for example, if a row has the value: "Argynnis childreni", I want the code to remove the second word so that I get the result "Argynnis".
Any ideas?
Here's some data. Specifically, I want to remove the 2nd word of all rows of the "species" column.
data.frame(
stringsAsFactors = FALSE,
species = c("Abraxaphantes sp",
"Abraxas sp","Acraea issoria","Actias sp","Agalope sp",
"Amesia sp"),
Individuals = c(2, 4, 19, 11, 7, 6),
Group = c("Other", "Other", "Butterfly", "Other", "Other", "Other"),
Group2 = c("insects/moths",
"insects/moths",NA,"insects/moths","insects/moths","insects/moths")
)
Thank you!!
Like this perhaps? I've put it in a new column - it removes the second word:
library(tidyverse)
df %>%
mutate(species2 = str_remove(species, "(\\s+\\w+)"))
species Individuals Group Group2 species2
1 Abraxaphantes sp 2 Other insects/moths Abraxaphantes
2 Abraxas sp 4 Other insects/moths Abraxas
3 Acraea issoria 19 Butterfly <NA> Acraea
4 Actias sp 11 Other insects/moths Actias
5 Agalope sp 7 Other insects/moths Agalope
6 Amesia sp 6 Other insects/moths Amesia
is it a possibility that the species might be 3 words ?
Yes, exactly like that. Thank you very much.
Yeah it is, but not in this particular case.
system
Closed
April 9, 2022, 6:14am
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.