-2

R beginner here.

The data from a relative humidity sensor malfunctioned and produced values over 100 and need to converted into NaNs. Any help is appreciated! Should I use ifelse?

This code hive1.1[hive1.1$int_h > 100] <- NaN produces this error:

Error in [<-.data.frame(*tmp*, hive2.1$int_h > 100, value = NaN) : missing values are not allowed in subscripted assignments of data frames

Evan
  • 217
  • 2
  • 13
  • Use `NA` instead of `NaN`. If that doesn't fix it, give us a bit of your data and the command you are using. Use `dput(head(your_object))` and paste it into the question. – Bryan Hanson Nov 21 '14 at 02:41
  • `NaN` is 'not a number' and is usually reserved for undefined return values though it is a little more complicated than that. `NA` simply means 'not available' i.e. no number. – Bryan Hanson Nov 21 '14 at 02:54
  • Apparently you didn't read the reproducible example link I left on [your other question](http://stackoverflow.com/questions/27052543/how-to-convert-0s-to-nans-in-certain-columns-of-dataframe-in-r) – Rich Scriven Nov 21 '14 at 03:01

2 Answers2

1

You need to focus your assignment better:

 hive1.1[hive1.1$int_h > 100, "int_h"] <- NaN 

Or:

  is.na(hive1.1[ , 'int_h"])  <- hive1.1$int_h > 100

Your code was not limiting the assignment to the column of interest.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

I'm still guessing a bit about your data, but maybe this:

hive1.1$int_h <- ifelse(hive1.1$int_h > 100, NA, hive1.1$int_h)

In words, check if the value is > 100, if so, replace it with NA, it not, leave it alone (or, more strictly, replace it with itself). See ?ifelse. ifelse is vectorized so it operates on each value in sequence.

Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78