dimandimab
dimandimab

Reputation: 21

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

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

Upvotes: 2

Views: 96

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389175

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)

Upvotes: 0

Scransom
Scransom

Reputation: 3335

Just index to the first 150 rows

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

Upvotes: 1

Related Questions