Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting Final Item from a List

Tags:

r

I have a list of names and would like to extract the last name of each individual. The complication is that some of the entries have middle names, some have nicknames, etc. Here's my example, building off of this question, but changing the formatting to reflect my situation:

df <- c("bob smith","mary ann d. jane","jose chung","michael mike marx","charlie m. ivan")

To get the first names, I use the following:

firstnames <- sapply(strsplit(df, " "), '[',1)

Is there any way to get the element in "final" position, however? Thanks in advance.

like image 458
bosbmgatl Avatar asked Dec 10 '25 09:12

bosbmgatl


1 Answers

> lastnames <- sapply(strsplit(df, " "), tail, 1)
> 
> lastnames
[1] "smith" "jane"  "chung" "marx"  "ivan" 
like image 163
IRTFM Avatar answered Dec 13 '25 20:12

IRTFM