[R] Conflating categories
Duncan Murdoch
murdoch at stats.uwo.ca
Fri Dec 14 14:59:44 CET 2007
On 12/14/2007 7:39 AM, Peter Paul wrote:
>
> Hi,
>
> I think this is a pretty basic question. I still couldn#t find the answer to
> it, though.
>
> I have some data loaded into R, which looks like this:
>
>> data()
> ...
> 38358 Advice Article
> 38359 Advice Article
> 38360 GeneralInfo List
> 38361 GeneralInfo Article
> 38362 Purchase Paragraphs
> 38363 Purchase List
> 38364 Purchase Paragraphs
> ...
>
>
> I now would like to edit the data in such a way that every "Advice" is
> changed into "GeneralInfo".
> Is there some easy way how I can do this?
This is a little tricky, because R has probably converted those columns
into factors. Let's assume that column 2 is named Type and the dataset
is named dat.
Then you replace it as
dat$Type[dat$Type == "Advice"] <- "GeneralInfo"
You'd think the following would work:
dat$Type <- ifelse(dat$Type == "Advice", "GeneralInfo", dat$Type)
but it doesn't, because ifelse() loses the factor levels. Factors are
one of the most error-prone features of the S language.
Duncan Murdoch
More information about the R-help
mailing list