[R] factors

From: Arnab mukherji <arnab_at_myrealbox.com>
Date: Fri 12 May 2006 - 05:48:32 EST


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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri May 12 07:48:17 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 12 May 2006 - 18:10:05 EST.

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