Dear r-users, Basically, I have a data as follows,> dataS s1 s2 s3 s4 s5 prob obs num.strata 1 NNNNN N N N N N 0.0000108 32 <NA> 2 NNNNY N N N N Y 0.0005292 16 <NA> 3 NNNYN N N N Y N 0.0005292 24 <NA> 4 NNNYY N N N Y Y 0.0259308 8 1 .... I want to replace <NA> by 0, when I tried the following command, I get som error message. data[is.na(data)]<-0 Warning message: In `[<-.factor`(`*tmp*`, thisvar, value = 0) : invalid factor level, NAs generated Anyone knows how to deal with this? Thanks, Kate [[alternative HTML version deleted]]
Hi Kate, is.na() does not work on entire data frames. You just need to specify the column, for example: data[is.na(data[, 8]), 8] <- 0 if the NAs were in column 8. Best regards, Josh On Tue, Nov 9, 2010 at 9:33 AM, Kate Hsu <yhsu.rhelp at gmail.com> wrote:> Dear r-users, > > Basically, I have a data as follows, > >> data > ? ? ? S s1 s2 s3 s4 s5 ? ? ?prob obs num.strata > 1 ?NNNNN ?N ?N ?N ?N ?N 0.0000108 ?32 ? ? ? <NA> > 2 ?NNNNY ?N ?N ?N ?N ?Y 0.0005292 ?16 ? ? ? <NA> > 3 ?NNNYN ?N ?N ?N ?Y ?N 0.0005292 ?24 ? ? ? <NA> > 4 ?NNNYY ?N ?N ?N ?Y ?Y 0.0259308 ? 8 ? ? ? ? ?1 > .... > > I want to replace <NA> by 0, when I tried the following command, I get som > error message. > data[is.na(data)]<-0 > > Warning message: > In `[<-.factor`(`*tmp*`, thisvar, value = 0) : > ?invalid factor level, NAs generated > > Anyone knows how to deal with this? > > Thanks, > > Kate > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Kate - As the error message indicates, num.strata is a factor. This can occur when you're reading in data and R encounters a non-numeric value which was not specified in the na.strings= argument to read.table. To do what you want, you'll need to convert it to a character variable first:> myvar = factor(c(NA,NA,NA,'1')) > myvar[is.na(myvar)] = 0Warning message: In `[<-.factor`(`*tmp*`, is.na(myvar), value = 0) : invalid factor level, NAs generated> myvar = as.character(myvar) > myvar[is.na(myvar)] = 0 > myvar[1] "0" "0" "0" "1" If you want the variable to be treated as a numeric variable, you can use as.numeric:> as.numeric(myvar)[1] 0 0 0 1 Hope this helps. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Tue, 9 Nov 2010, Kate Hsu wrote:> Dear r-users, > > Basically, I have a data as follows, > >> data > S s1 s2 s3 s4 s5 prob obs num.strata > 1 NNNNN N N N N N 0.0000108 32 <NA> > 2 NNNNY N N N N Y 0.0005292 16 <NA> > 3 NNNYN N N N Y N 0.0005292 24 <NA> > 4 NNNYY N N N Y Y 0.0259308 8 1 > .... > > I want to replace <NA> by 0, when I tried the following command, I get som > error message. > data[is.na(data)]<-0 > > Warning message: > In `[<-.factor`(`*tmp*`, thisvar, value = 0) : > invalid factor level, NAs generated > > Anyone knows how to deal with this? > > Thanks, > > Kate > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
On Tue, Nov 9, 2010 at 9:39 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:> Hi Kate, > > is.na() does not work on entire data frames.whoops, I did not mean that. is.na() has a data frame method, but there are assignment issues (as you saw) when you use it that way. Josh [snip]
On Tue, Nov 9, 2010 at 9:43 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:> On Tue, Nov 9, 2010 at 9:39 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote: >> Hi Kate, >> >> is.na() does not work on entire data frames. > > whoops, I did not mean that. ?is.na() has a data frame method, but > there are assignment issues (as you saw) when you use it that way.Last correction (I promise). Somehow I tested incorrectly which lead to me to my two erroneous statements before. is.na() works just fine with data frames. It was just the factor issue (as Phil said).> x <- data.frame(a = 1:3, b = c(1, 2, NA), d = c(NA, 2, 3), e = factor(c(NA, NA, "1"))) > xa b d e 1 1 1 NA <NA> 2 2 2 2 <NA> 3 3 NA 3 1> x[is.na(x)] <- 0Warning message: In `[<-.factor`(`*tmp*`, thisvar, value = 0) : invalid factor level, NAs generated> x # other NAs removed, but not the factor onesa b d e 1 1 1 0 <NA> 2 2 2 2 <NA> 3 3 0 3 1 off-to-hide-under-a-rock-Josh
Hi r-help-bounces at r-project.org napsal dne 09.11.2010 18:33:54:> Kate Hsu <yhsu.rhelp at gmail.com> > Odeslal: r-help-bounces at r-project.org > > 09.11.2010 18:33 > > Komu > > r-help at r-project.org > > Kopie > > P?edm?t > > [R] Question regarding to replace <NA> > > Dear r-users, > > Basically, I have a data as follows, > > > data > S s1 s2 s3 s4 s5 prob obs num.strata > 1 NNNNN N N N N N 0.0000108 32 <NA> > 2 NNNNY N N N N Y 0.0005292 16 <NA> > 3 NNNYN N N N Y N 0.0005292 24 <NA> > 4 NNNYY N N N Y Y 0.0259308 8 1 > .... > > I want to replace <NA> by 0, when I tried the following command, I getsom> error message. > data[is.na(data)]<-0 > > Warning message: > In `[<-.factor`(`*tmp*`, thisvar, value = 0) : > invalid factor level, NAs generatedIt is because num.strata is probably factor see str(data) however there is usually no need for changing NA to zero as many (most, all?) functions have some kind of clever handling of missing values represented by NA. regards Petr> > Anyone knows how to deal with this? > > Thanks, > > Kate > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.