Re: [R] Conflating categories

From: Duncan Murdoch <>
Date: Fri, 14 Dec 2007 08:59:44 -0500

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 mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Fri 14 Dec 2007 - 14:06:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 14 Dec 2007 - 14:30:19 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.