简体   繁体   中英

Selecting Final Item from a List

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.

> lastnames <- sapply(strsplit(df, " "), tail, 1)
> 
> lastnames
[1] "smith" "jane"  "chung" "marx"  "ivan" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM