R function for removing first 4 characters in a column?

Viewed 528

I have a data frame (Data) and I have created a new column with the first four characters of each vector in a column (Details) using substr.

Here is the code:

Data$Years = substr(Data$Details, 1, 5)

How would I use substr (or another function) to remove the same first four characters from the original (Details) column?

1 Answers

We can specify the first (in substring) or start in substr as the 5th character and last (not needed in substring as it is by default last = 1000000L) or stop in substr as the last character (nchar)

substring(Data$Details, 5)

Or with substr

substr(Data$Details, 5, nchar(Data$Details))

Or using a regex to match any character (. - repeated 4 times) from the start (^) of the string and replace with blank ("")

sub("^....", "", Data$Details)
Related