3

I would like to remove the last 4 characters for the column "Name" for every row. This is what I have tried.

hof_pitching.final[, Name := substring(hof_pitching.final$Name, 0, (length(hof_pitching.final$Name) - 4))]

Any help would be appreciated.

Bruce Pucci
  • 1,821
  • 2
  • 19
  • 26
  • 1
    I don't have your data to test this, but my first thought is that you don't need to reference `hof_pitching.final$Name` inside a `data.table` object. You can just go straight for `Name` as `data.table` interprets variables within the scope of the table. – thelatemail May 26 '14 at 23:11

1 Answers1

9

Perhaps you're looking for nchar?

require(data.table)
DT <- data.table(x=1:5, y=paste0("V", 10000:10004))
DT[, z := substring(y, 0, nchar(y)-4L)]
# > DT
#    x      y  z
# 1: 1 V10000 V1
# 2: 2 V10001 V1
# 3: 3 V10002 V1
# 4: 4 V10003 V1
# 5: 5 V10004 V1
Arun
  • 116,683
  • 26
  • 284
  • 387