0

I'm working in R. I have a dataset with people first and last names. There is a column called "First" and another column called "Last".

enter image description here

I want to change "Bodie" to just "B" and do the same for all the observations in the "Last" column.

I'm newer to programming so I don't even know where to start. I have looked at some of the string packages in R and can't quite figure out what to do. Thanks for the help.

p.habermanm
  • 85
  • 1
  • 9

2 Answers2

0

We can use substr to extract the first letter of the 'Last' column

df1$Last <- substr(df1$Last, 1, 1)

Or sub to remove all the characters other than the first

df1$Last <- sub("^(.).*", "\\1", df1$Last)

Or another option is to split the characters, select the first element

df1$Last <- sapply(strsplit(df1$Last, ""), `[`, 1)
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Just a variation on the @akrun answer which uses sub sans a capture group:

df1$Last <- sub("(?<=.).*$", "", df1$Last, perl=TRUE)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360