0

So my data is like :

sample3
 dx dxgroup ID
110.1   0   3018njjkk00
110.1   0   3233jjkk000
110.3   0   1697jj99000
110.3   0   16976700000
110.5   0   27320000000
110.8   110.0   30180000061
112 0   17910000579

I want to update dxgroup column like :

(pseudocode)

* if dxgroup !== 0 ; then dxgroup = dxgroup
* else dxgroup == 0 ;
    If dx (fisrt 4 characters match with)= (list1)
        then dxgroup = (first 4 character of dx)
    else dx (fisrt 3 characters match with) = (list2)
        then dxgroup = (first 3 character of dx)
else dxgroup = dx
)

I have tried following:

  • I have creacted all the list which needed to be match against like: list1<-as.character(c("209","493","682","850","V72"))

My code is :

 sample3[ , dxgroup := { ifelse (sample3$dxgroup == 0,  
 (ifelse(((substring(sample3$dx[], 1, 5)) %in% list1), substring(sample3$dx[], 1, 5),
  ifelse(((substring(sample3$dx[], 1, 3)) %in% list2) ||((substring(sample3$dx[], 1, 5)) %in% list3), substring(sample3$dx[], 1, 5),
  ifelse(((substring(sample3$dx[], 1, 3)) %in% list 5),'042',
  ifelse(((substring(sample3$dx[], 1, 3)) %in% list6) &!((substring(sample3$dx[], 1, 5)) %in% list7), '789.0',
  ifelse(((substring(sample3$dx[], 1, 5)) %in% list8) &!((substring(sample3$dx[], 1, 5)) %in% list9), '786.50',sample3$dx[])))))),
  sample3$dxgroup[])}]

But I am getting error:

> sample3[ , dxgroup := { ifelse (sample3$dxgroup == 0,  
+                                 (ifelse(((substring(sample3$dx[], 1, 5)) %in% list1), substring(sample3$dx[], 1, 5),
+                                         ifelse(((substring(sample3$dx[], 1, 3)) %in% list2) ||((substring(sample3$dx[], 1, 5)) %in% list3), substring(sample3$dx[], 1, 5),
+                                                ifelse(((substring(sample3$dx[], 1, 3)) %in% list 5),'042',
Error: unexpected numeric constant in:
"                          ifelse(((substring(sample3$dx[], 1, 3)) %in% list2) ||((substring(sample3$dx[], 1, 5)) %in% list3), substring(sample3$dx[], 1, 5),
                                               ifelse(((substring(sample3$dx[], 1, 3)) %in% list 5"

(and then go on to get another error...)

>                                                       ifelse(((substring(sample3$dx[], 1, 3)) %in% list6) &!((substring(sample3$dx[], 1, 5)) %in% list7), '789.0',
+                                                              ifelse(((substring(sample3$dx[], 1, 5)) %in% list8) &!((substring(sample3$dx[], 1, 5)) %in% list9), '786.50',sample3$dx[])))))),
Error: unexpected ')' in:
") %in% list6) &!((substring(sample3$dx[], 1, 5)) %in% list7), '789.0',
ifelse(((substring(sample3$dx[], 1, 5)) %in% list8) &!((substring(sample3$dx[], 1, 5)) %in% list9), '786.50',sample3$dx[])))"

(and another...)

>                                 sample3$dxgroup[])}]
Error: unexpected ')' in "                                sample3$dxgroup[])"
Frank
  • 66,179
  • 8
  • 96
  • 180
n.datascience
  • 51
  • 1
  • 3
  • I think the problem is your use of `list 5`. Try running it again without the space between those two. – Frank Dec 11 '13 at 22:13
  • possible duplicate of [Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code](http://stackoverflow.com/questions/25889234/error-unexpected-symbol-input-string-constant-numeric-constant-special-in-my-co) – tonytonov Sep 17 '14 at 14:02

0 Answers0