Re: [R] Changing values (factors) does not change levels of that value?!

From: Oliver Bandel <oliver_at_first.in-berlin.de>
Date: Sun, 16 Nov 2008 14:52:10 +0100

Zitat von "Weiss, Bernd " <bernd.weiss_at_uni-koeln.de>:

> Philipp Pagel schrieb:
> >> * when then looking at str(weblog),
> >> the "-" will stay in the levels, mentioned for the variable
> weblog$V8
> >> -> BAD!
> >>
> >> Is this snormal behaviour?
> >
> > Yes, it is. The idea is that a factor has a given set of levels
> > independent of how often you find them in your data - including
> > the case that a level is not observed at all. E.g. gender cn take
> > levels 'male' or 'female' but you may have a sample of females.

OK, but I thought, when touching the data, it will recalculate the levels. Now I see, it does not. I found a function "relevel", but it does not help me.

> >
> >> Do I have to throw out the unwanted level by myself?
> >
> > Yes, and it's easy:
> >
> >> x <- factor(c('A','B','C','A','C'))
> >> y <- x[x!='C']
> >> y
> > [1] A B A
> > Levels: A B C
> >> factor(y)
> > [1] A B A
> > Levels: A B

Sorry, this looks to me like you throw out all the values, where the unwanted attribute is. (?!)
That is not what I meant. Or at least it's disturbing because you use one value, not working on a data-frame, as I do.

After some experimentation I found out the following solution:



weblog <- read.table("web.log") # reading the log

weblog$V8[ weblog$V8 == "-" ] <- 0 # substituting "-" by 0

# and now changing the levels-attribute to the new values !! attr(weblog$V8, "levels") <- levels( factor( as.vector(weblog$V8) ) )


But after I found that, I saw, that this was a detour from what I tried when I started, and now using I do the following:



weblog <- read.table("web.log") # read in the weblog

weblog$V8[ weblog$V8 == "-" ] <- 0 # substituting "-" by 0

weblog$V8 <- as.numeric( as.vector(weblog$V8) ) # changing it to numeric

tapply( weblog$V8, weblog$V1, sum) # do my calculations


Ciao,

   Oliver



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 16 Nov 2008 - 13:54:19 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 Sun 16 Nov 2008 - 15:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive