Im trying to categorising my data into different group based on type of data. My data and code is as follow:
bank ROE
bank1 0.73
bank2 0.94
bank3 0.62
b
You should use the %in%
-operator instead of the identity--you are comparing against a vector here.
Like so:
test$type <- ifelse(test$bank %in% sob, 1, ifelse(test$bank %in% fob, 2, ifelse(test$bank %in% jov, 3, 4)))
> test
bank ROE type
1 bank1 0.73 1
2 bank2 0.94 1
3 bank3 0.62 1
4 bank4 0.57 2
5 bank5 0.31 2
6 bank6 0.53 2
7 bank7 0.39 3
8 bank8 0.01 3
9 bank9 0.16 3
10 bank10 0.51 3
11 bank11 0.84 3
12 bank12 0.18 4
Alternatively, to avoid the cumbersome if-else structures you could do the classification resetting levels of a factor.
first copy the bank variable test$type<-test$bank
then, re-set the levels, using the vectors defined above (sob, fob, job). Notice the last step, 'other'
is set to the remaining value because bank12 is not defined in the other vectors.
levels(test$type) <- list('sob' = sob,
'fob' = fob,
'jov' = jov,
'other' = 'bank12')
Resulting in
> test
bank ROE type
1 bank1 0.73 sob
2 bank2 0.94 sob
3 bank3 0.62 sob
4 bank4 0.57 fob
5 bank5 0.31 fob
6 bank6 0.53 fob
7 bank7 0.39 jov
8 bank8 0.01 jov
9 bank9 0.16 jov
10 bank10 0.51 jov
11 bank11 0.84 jov
12 bank12 0.18 other