1

Most of the values in a variable of my dataset has two extra characters at the end to reprensent the number of visits from the subject, like id_0, or id_24, or id_48... Like in the below:

  subjectid                   le                 lt
1  CC0005_0                 4022.5             133.8
2  CC0196_0                 4099.6             190.7
3  CC0197_0                 5518.1             595.0
4  CC0202_48                 6604.3             358.2
5  CC0212_0                 7047.3             427.8
6  CC0239_24                 4935.2             215.7

My first thought was to use apply and grepl together, but I couldn't go past:grepl("*_0", mydata$subjectid), this seems to catch the correct items, but how to rename them?

  • How can I get rid of "_0" without changing the ones that have "_24" or "_48"?
lf_araujo
  • 1,991
  • 2
  • 16
  • 39

1 Answers1

1

We can use sub to match the _ followed by 0 at the end ($) of the string and replace it with blank ("")

df1$subjectid <- sub("_0$", "", df1$subjectid)
df1$subjectid
#[1] "CC0005"    "CC0196"    "CC0197"    "CC0202_48" "CC0212"    "CC0239_24"
akrun
  • 874,273
  • 37
  • 540
  • 662