S******y 发帖数: 1123 | 1 I have a numberical field in an R data frame, say x, which has many values -
1, 2, 3, ..., 999
Each numerical value corresponds to a categorical text value, say -
1 -> 'apple'
2 -> 'pear'
3 -> 'grape'
...
999 ->'other'
In SAS, PROC FORMAT would be very handy to take care of mapping original
value (e.g. 1) to translated values (e.g. 'apple').
But in R, what is the most effiecient way to do this?
thanks! | D******n 发帖数: 2836 | 2 a<-c(1,1,2,3,1,2,3);
mapping<-c('apple','pear','grape');
na<-mapping[a];
-
【在 S******y 的大作中提到】 : I have a numberical field in an R data frame, say x, which has many values - : 1, 2, 3, ..., 999 : Each numerical value corresponds to a categorical text value, say - : 1 -> 'apple' : 2 -> 'pear' : 3 -> 'grape' : ... : 999 ->'other' : In SAS, PROC FORMAT would be very handy to take care of mapping original : value (e.g. 1) to translated values (e.g. 'apple').
| S******y 发帖数: 1123 | 3 Thanks! DaShagen.
That works great!
One more question, what if I need to do some grouping - e.g -
1,2 -> 'good fruit'
2,3,5 --> 'bad fruit'
...
999 -> 'other fruit'
? | D******n 发帖数: 2836 | 4 mapping<-c('good fruit','good fruit','bad fruit','bad fruit'....)
hehe this looks silly though.
【在 S******y 的大作中提到】 : Thanks! DaShagen. : That works great! : One more question, what if I need to do some grouping - e.g - : 1,2 -> 'good fruit' : 2,3,5 --> 'bad fruit' : ... : 999 -> 'other fruit' : ?
| S******y 发帖数: 1123 | 5 Thanks. That was helpful!
It would be nice if , for general purpose of recoding variable values, R has
a 'DECODE' function like in SQL. |
|