Right now, I have dataset consisting of variables Gbcode
and ncnty
> str(dt)
'data.frame': 840 obs. of 8 variables:
$ Gbcode : Factor w/ 28 levels "11","12","13",..: 21 22 23 24 25 26 27 28 16 17 ...
$ ncounty : num 0 0 0 0 0 0 0 0 0 0 ...
I want to do the following thing:
if a data record is with Gbcode
equal to 11, then assign 20 to its ncnty
Gbcode
: 11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33
Corresponding ncnty
: 20, 19, 198, 131, 112, 102, 60, 145, 22, 115, 95
I am wondering whether there is any better solution rather than write an if
statement, which would be with many lines in this case, maybe less than 20 lines of code.