removing the first digit of a string for the first n rows in a column

Viewed 44

I have a long column with about 5000 rows and each row has 6 digits. I would like to remove the first digit of the first 150 rows. How do I do that?

I tried this function:

gsub("^[0-9]","", f1992$cleaned1992)

f1992 is the dataframe and cleaned1992 is the column that I am working with. The problem is the code removes the first digit for all the 5000 rows.

Screenshot of the data

2 Answers

Just index to the first 150 rows

 gsub("^[0-9]","", f1992$cleaned1992[1:150]) 

With sub you can do

f1992$cleaned1992[1:150] <- sub(".", "", f1992$cleaned1992[1:150])

Or using substring

f1992$cleaned1992[1:150] <- substring(f1992$cleaned1992[1:150], 2)
Related