Extract first word from a column and insert into new column
We can use function stringr::word
:
library(stringr)
Dataframe1$COL2 <- word(Dataframe2$COL1, 1)
You can use a regex ("([A-Za-z]+)"
or "([[:alpha:]]+)"
or "(\\w+)"
) to grab the first word
Dataframe1$COL2 <- gsub("([A-Za-z]+).*", "\\1", Dataframe1$COL1)
The function strsplit
can be useful
Dataframe1$COL2 <- strsplit(Dataframe1$COL1, " ")[[1]][1]
Then you can change the last bracketed number to select other parts from the string too.