Skip to content

Conflating categories

3 messages · mika03, PIKAL Petr, Duncan Murdoch

#
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:
...
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?

Thanks a lot!
#
Hi

r-help-bounces at r-project.org napsal dne 14.12.2007 13:39:02:
answer to
If it is factor you can change its levels
[1] A01 A01 A01 A02 A02 A03 A03 A04
Levels: A01 A02 A03 A04
[1] "A01" "A02" "A03" "A04"
[1] one one one one one A03 A03 A04
Levels: one A03 A04

Regards
Petr
http://www.R-project.org/posting-guide.html
#
On 12/14/2007 7:39 AM, Peter Paul wrote:
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