1

I have a mid-sized data frame with two busted rows. The addresses are bad, so geocode() didn't geocode them and so I don't have lat/long info for either.

I tried making a temp frame to play with:

tmp.frame <- subset(all.sites, is.na(all.sites$lat))

I can update values in tmp.frame with tmp.frame$Address <- c("Better Address 1","Better Addresss 2") but I can't seem to edit the value in the larger frame. all.sites[392, 7] is one of the spots with missing lat/lon and I thought that all.sites[392, 7] = c("Better Addy") would do the trick, but instead I'm getting this:

Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = "Better Addy") :
  invalid factor level, NA generated

Curiously, tmp.frame[2,4] = c("Better") works just fine. So I'm not sure what the difference between the two is.

Note: when I try vi(all.sites[392,7]) I get this, roughly:

structure(NA_integer_, .Label = c("10003 39th Avenue",..., class = "factor")
Community
  • 1
  • 1
Amanda
  • 12,099
  • 17
  • 63
  • 91
  • 1
    I believe the problem may be that some of your columns are being converted to factors, rather than numeric or characters. This may be relevant if that's the case: http://stackoverflow.com/questions/2851015/convert-data-frame-columns-from-factors-to-characters – Csislander Apr 22 '14 at 13:34
  • Yeah, you're trying to put a character class value into what is likely a factor class column. Perhaps `tmp.frame$Address <- as.factor(c("Better Address 1","Better Addresss 2"))` would work – sckott Apr 22 '14 at 13:35
  • @Csislander Sounds about right. Thanks. – Amanda Apr 22 '14 at 13:37

1 Answers1

4

That happens because the column you are trying to update is a factor, which by definition has a limited domain (set of possible values). When you try to force an entry to be a level that was not part of this domain, this produces a warning and an NA. Try this:

ff <- factor(sample(1:3, size=20, replace=TRUE))  ## factor of 1, 2, and 3
ff[5] <- 4    ## try changing the fifth entry to an 'unknown' value
levels(ff)    ## show set of known values

If you want to be able to modify the values to anything you like, you need to convert the factor to a character, or integer (depending on what it represents). Only change to a factor after you are confident all possible values are there. You can also specify them manually:

ff2 <- factor(sample(1:3, size=20, replace=TRUE), levels=1:4)  ## 1-4 are legal
ff2[5] <- 4    ## accepted without problems
head(ff2)      ## check 
ilir
  • 3,236
  • 15
  • 23