How to update existing column values ​​in data.table?

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

I have 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 30,000 lines.

  • I want to apply this logic:

    If dxgroup = NA(
        If dx (fisrt 4 characters match with)= (2024, 2967, 9786,9788,8263)
            then dxgroup = (first 4 character of dx)
        else dx (fisrt 3 characters match with) = (V70, 042,897)
            then dxgroup = (first 3 character of dx)
    else dxgroup = dx
    )
    
  • The result should look like this:

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

  • I apologize: this is the first time I'm asking something here, so I'm not used to it yet. So, I did something like this (I do not, if it is correct, and I also had an error):

    sample4 <-sample3 [, dxgroup: = {if (dxgroup == NA)

    • {if (substring (sample3 $ dx, 1,4) == list (2501,2780,4151,5301,5751,6860,7807,7890,9898,9955,9970)) substring (sample3 $ dx, 1, 4)
    • else if (substring (sample3 $ dx, 1,3) == list (042,493,682,850, V72)) substring (sample3 $ dx, 1,3)
    • else if (substring (sample3 $dx, 1,4) == list (8540, 8541)) substring (sample3 $dx, 1,3)
    • else if (substring (sample3 $dx, 1,3) == list (043, 044)) 042
    • else if (substring (sample3 $dx, 1,3) == list (789) (sample3 $dx, 1,3)!= list (7891,7893,78930)) 7890
    • else if (substring (sample3 $dx, 1,4) == list (7865) (sample3 $dx, 1,4)!= list (78651,78652,78659)) 78650}
    • else sample3 $dx}] if (dxgroup == NA) {: , TRUE/FALSE : : if (dxgroup == NA) {: > 1,
+4
2

.

, data.table(, R) j {curly brackets}, - , . :

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

data.table:

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

, , script .

( , data.table . data.tables data.frames, , .)

+2

All Articles