n.datascience
n.datascience

Reputation: 51

How to update existing column values in data.table?

I just started this programming, apologize for asking this simple question but I am stuck.

I have a data.table called s3:

s3:

ClaimID           dx      dxgroup
15nhbfcgcda       113.8   NA
15nhbfcgcda       156.8   NA
15nhbfcgcda       110.8   059
15nhbfcfssa       135.8   NA
15nhb4dfgda       V70.3   NA
15nhbf644da       118.8   042

S3 has 30000 rows.

Upvotes: 5

Views: 8394

Answers (2)

Zach
Zach

Reputation: 30331

This is a more data.table friendly solution:

library(data.table)
s3 <- data.table(s3)
s3[is.na(dxgroup) & (substring(ClaimID, 1, 3) %in% ("V70", "042", "897")), dxgroup := substring(dx, 1, 3)]
s3[is.na(dxgroup) & (substring(ClaimID, 1, 4) %in% ("2024", "2967", "9786", "9788", "8263")), dxgroup := substring(dx, 1, 4)]
s3[is.na(dxgroup), dxgroup := dx] #Default

Basically, you work from the most specific condition to the most global condition, as each line in the above script potentially overwrites matches from the previous line.

(I assume you're using the data.table package. data.tables are different than data.frames, and in my opinion much better.)

Upvotes: 3

Ricardo Saporta
Ricardo Saporta

Reputation: 55420

You have the logic all set.

Note that with data.table (well, almost all of R), you can wrap the j in {curly brackets} and the final statement in the brackets is what will be assigned. eg:

DT[,  dxgroup :=  { if (clause1)  
                     {if (foo) beebar else bar}
                  else chewybar
                  } 
  ]

Upvotes: 7

Related Questions