[R] factors

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri May 12 08:44:12 CEST 2006


On Thu, 11 May 2006, Arnab mukherji wrote:

> Hi R-users,
>
> I had a minor issue while demonstating R that I can't explain. I am 
> hoping someone will have suggestions. The only difference is a call to 
> fix() in between that I made to ensure people were following me. 
> However, that seems to have altered the way R code got executed.
>
> I was wondeirng if people have any insights.

You did not create a factor, and in the first version 'pass' is character.
edit.data.frame (as used by fix) did some coercion.

It would be better to do

dat$pass <- factor(rep("Fail", 10), levels=c("Fail", "Pass"))
dat[ ind, "pass"] <- "Fail"
dat[ !ind, "pass"] <- "Pass"

and also rather than fix(dat), do

invisible(edit(dat))

which does not save changes.


>
> Arnab
>
>
>
> # When it does work
>     x <-letters[1:10]
>     marks <-runif(10)*100
>     dat <- data.frame(Student.id = x, Grade = rep(1:5,2), Marks = marks)
>     ind <- dat$Marks  < 50
>     dat[ ind, "pass"] <- "Fail"
>     dat[!ind, "pass"] <- "Pass"
>     dat
>  Student.id Grade    Marks pass
> 1           a     1 81.91246 Fail
> 2           b     2 67.66341 Pass
> 3           c     3  4.05249 Pass
> 4           d     4 94.37248 Fail
> 5           e     5 50.83555 Pass
> 6           f     1 16.34850 Fail
> 7           g     2 43.68585 Pass
> 8           h     3 49.67190 Fail
> 9           i     4 74.41275 Fail
> 10          j     5 86.15475 Fail
>
>
>
> # When it Doesnÿÿt Work
>
>>     x <-letters[1:10]
>>     marks <-runif(10)*100
>>     dat <- data.frame(Student.id = x, Grade = rep(1:5,2), Marks = marks)
>>     ind <- dat$Marks  < 50
>>     dat[ ind, "pass"] <- "Fail"
>>
>> fix(dat)
>>     dat[!ind, "pass"] <- "Pass"
> Warning message:
> invalid factor level, NAs generated in: "[<-.factor"(`*tmp*`, iseq, value = c("Pass", "Pass", "Pass",
>
>> dat
>  Student.id Grade    Marks pass
> 1           a     1 72.14612 <NA>
> 2           b     2 64.34044 <NA>
> 3           c     3 20.27918 Fail
> 4           d     4 39.01875 Fail
> 5           e     5 92.19682 <NA>
> 6           f     1 58.74779 <NA>
> 7           g     2 35.31430 Fail
> 8           h     3 18.64664 Fail
> 9           i     4 51.58880 <NA>
> 10          j     5 77.94396 <NA

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list