[R] factors

Arnab mukherji arnab at myrealbox.com
Thu May 11 21:48:32 CEST 2006


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.

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




More information about the R-help mailing list