0

I have a data frame with measurements.

One column show the measurements in mm, and the other in units (which is a relative scale varying with zoom-level on the stereo microscope I was using). I want to go through every row of my data frame, and for each "length_mm" that equals NA, I want to use the value in "length_units" to calculate "length_mm".

So far I've tried this code:

    convert_to_mm <- function(x) {
      x["length_mm"==NA] <- x["length_units"]/10
      x["length_mm"]
    }
    apply(zooplankton[,c("length_mm","length_units")], 1, convert_to_mm)

Sometimes I also have instances where both "length_mm" and "length_units" equals NA. In these cases I want to just skip the row.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Harald
  • 65
  • 1
  • 1
  • 8
  • 1
    This looks wrong: `x["length_mm"==NA] <- ...`. You should test your functions on single instances before trying to use them inside an apply procedure. – IRTFM May 15 '15 at 20:14
  • It frequently helps to get an answer faster if you provide [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with some sample data. – akhmed May 15 '15 at 20:18

2 Answers2

3

This is easier than you're making it

# Which are the rows with bad values for mm? Create an indexing vector:
bad_mm <- is.na(zooplankton$length_mm)

# Now, for those rows, replace length_mm with length_units/10
zooplankton$length_mm[bad_mm] <- zooplankton$length_units[bad_mm]/10

Remember to use is.na(x) instead of x==NA when checking for NA vals. Why? Take a look at NA==NA for a hint!

arvi1000
  • 9,393
  • 2
  • 42
  • 52
0

Almost there. You should use is.na(myVariable) and not myVariable ==NA

User7598
  • 1,658
  • 1
  • 15
  • 28
cmbarbu
  • 4,354
  • 25
  • 45