Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I am having a dataframe that is having columns of names and surnames. I need to eliminate all the first names.

Sociologist             Referencia

1  Peter Abell            Peter Abell

2  Mark Abrams            Mark Abrams

3  Janet Abu-Lughod       Janet Abu-Lughod

4  Jane Addams            Jane Addams

5  Theodor W. Adorno      Theodor W. Adorno

6  Richard Alba           Richard Alba

I tried with a code taken from a similar question, but it eliminates the surnames, not the first names, which is what i need. Below is the code that I have implemented:

Sociologos_df$word<- sub("([A-Za-z]+).*", "\1", Sociologos_df$word)

1 Answer

0 votes
by (108k points)

For easy implementation, I would suggest using the word() from stringr in R programming which helps to extract the last word in each name assuming that is going to be the surname.

stringr::word(df$Sociologist, -1)

#[1] "Abell"      "Abrams"     "Abu-Lughod" "Addams"     "Adorno"     "Alba" 

For the majority of cases, this code might work.

#the data

df <- structure(list(Sociologist = c("Peter Abell", "Mark Abrams", 

"Janet Abu-Lughod", "Jane Addams", "Theodor W. Adorno", "Richard Alba"

)), class = "data.frame", row.names = c(NA, -6L))

Related questions

0 votes
1 answer
0 votes
1 answer

Browse Categories

...