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
  • 704,970
  • 168
  • 1,303
  • 1,425
Harald
  • 65
  • 1
  • 1
  • 7
  • 1
    This looks wrong: `x["length_mm"==NA] – 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
  • 8,833
  • 1
  • 36
  • 50
0

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

User7598
  • 1,638
  • 1
  • 13
  • 27
cmbarbu
  • 4,146
  • 24
  • 43