Home > Software engineering >  How to trim the first digits of a column of my data frame?
How to trim the first digits of a column of my data frame?

Time:07-07

How to trim the first digits of a column of my data frame?

This is the dput(head(df2$y))

c("1. Neodiprion virginianus", "2. Nepsalus jezoensis", "3. Prochas sp. 2 YYH-2022a", 
"4. Prochas sp. 1 YYH-2022a", "5. Eccoptopterus sp. 1 CP-2022", 
"6. Andricus sp. 1 CYS-2022a")

and

dput(tail(df2$y))

c("761849. Libellula", "761850. Libellulidae", "761851. Anisoptera", 
"761852. Odonata", "761853. Euscelis plebejus", "761854. Euscelis")

I need something like this:

c("Neodiprion virginianus", "Nepsalus jezoensis", "Prochas sp. 2 YYH-2022a", 
"Prochas sp. 1 YYH-2022a", "Eccoptopterus sp. 1 CP-2022", 
"Andricus sp. 1 CYS-2022a")

and

c("Libellula", "Libellulidae", "Anisoptera", 
"Odonata", "Euscelis plebejus", "Euscelis")

Respectively

Many thanks!

CodePudding user response:

Try this

x <- c("1. Neodiprion virginianus", "2. Nepsalus jezoensis", "3. Prochas sp. 2 YYH-2022a", 
       "4. Prochas sp. 1 YYH-2022a", "5. Eccoptopterus sp. 1 CP-2022", 
       "6. Andricus sp. 1 CYS-2022a")



x <- gsub("\\d \\. " , "" , x)

so for your data use

df2$y <- gsub("\\d \\. " , "" , df2$y)
  • Output
[1] "Neodiprion virginianus"      "Nepsalus jezoensis"         
[3] "Prochas sp. 2 YYH-2022a"     "Prochas sp. 1 YYH-2022a"    
[5] "Eccoptopterus sp. 1 CP-2022" "Andricus sp. 1 CYS-2022a" 
[1] "Libellula"         "Libellulidae"      "Anisoptera"       
[4] "Odonata"           "Euscelis plebejus" "Euscelis"     
  •  Tags:  
  • r
  • Related