Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract first word from a column and insert into new column [duplicate]

I have a dataframe below and want to extract the first word and insert it into a new column

Dataframe1:

COL1
Nick K Jones
Dave G Barros
Matt H Smith

Convert it to this:

Dataframe2:
COL1              COL2
Nick K Jones      Nick
Dave G Barros     Dave
Matt H Smith      Matt
like image 461
Nick Avatar asked Aug 10 '15 17:08

Nick


People also ask

How do I get the first word of a string in R?

Example 3: Get First Word of Character String Using word() Function of stringr Package. Similar to Example 2, we can use the word() function to find the first word of a character string. The first word of our character string is “have”.

How would you extract one particular word from a string in R?

To extract words from a string vector, we can use word function of stringr package. For example, if we have a vector called x that contains 100 words then first 20 words can be extracted by using the command word(x,start=1,end=20,sep=fixed(" ")).


3 Answers

We can use function stringr::word:

library(stringr)  Dataframe1$COL2 <- word(Dataframe2$COL1, 1) 
like image 197
Colibri Avatar answered Oct 16 '22 17:10

Colibri


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) 
like image 38
Rorschach Avatar answered Oct 16 '22 16:10

Rorschach


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.

like image 35
mattbawn Avatar answered Oct 16 '22 16:10

mattbawn