Amanda
Amanda

Reputation: 12757

Why can't I edit values in this data frame?

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")

Upvotes: 1

Views: 2492

Answers (1)

ilir
ilir

Reputation: 3224

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 

Upvotes: 4

Related Questions