简体   繁体   中英

R function for removing first 4 characters in a column?

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?

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)

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