所以我的数据就像:
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
我想更新dxgroup列,如:
(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
)
我试过以下:
list1<-as.character(c("209","493","682","850","V72"))
我的代码是:
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[])}]
但我收到错误:
> 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"
(然后继续得到另一个错误......)
> 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[])))"
(和另一个......)
> sample3$dxgroup[])}]
Error: unexpected ')' in " sample3$dxgroup[])"