-2

I am trying to write a conditional statement in order to delete all the temperature values above 27 degrees.

I wrote this line of code, but it is not working:

for (i in 1:length(New_DF$Temperature)) {
  if(New_DF$Temperature[i] > 27) {
    New_DF$Temperature <- NA
  }
}

And I would like to delete the whole row that has a temperature above 27 degrees.

Thank you!

Leonardo
  • 2,439
  • 33
  • 17
  • 31
N.A.K
  • 35
  • 6

2 Answers2

3

Welcome to Stack Overflow. You will find you get better answers if you do some research first, both on SO norms and on R itself. This is basic R, covered in the first chapter or two of any introductory text.

For instance, your question should be reproducible: How to make a great R reproducible example?

With that said, rather than trying to write a loop to do this, use a vector to select the items you need, and delete them from the data.frame:

selector <- New_DF$Temperature>27
New_DF <- New_DF[ !selector, ]

Or in one line:

New_DF[ !New_DF$Temperature>27, ] 
Community
  • 1
  • 1
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • Thank you for the help but this actually deletes all the temperature values below 27 and keep the temperature values above 27 in the data frame. How can I do the opposite? – N.A.K Feb 04 '17 at 13:04
  • 1
    Again, just read a basic introduction. If you take away the NOT operator (the !), it will invert. Or flip the > to a <. – Ari B. Friedman Feb 04 '17 at 13:41
  • Exactly, but why isn't it diving me the data with temperature less than 27? – N.A.K Feb 04 '17 at 14:07
2

Easier just to filter your data set. No need for a loop.

library(dplyr)

filtered_DF <- filter(New_DF, Temperature < 27)

you can also use base R's subset

filtered_DF <- subset(New_DF, Temperature < 27)
Matt W.
  • 3,692
  • 2
  • 23
  • 46
  • when I run your line of code I get '<' not meaningful for factors even though Temperature is numeric. Do you know why I get this? Many thanks! – N.A.K Feb 04 '17 at 18:57
  • str(New_DF) and see what classes everything is. let me know what it comes out as – Matt W. Feb 04 '17 at 19:03
  • I checked, it tells me that all my variables are factors, however when I use mode(New_DF$Temperature) I get that Temperature is numeric – N.A.K Feb 04 '17 at 19:06
  • 1
    use class(New_DF$Temperature) to find out what class it is. str(New_DF) is your best friend to see what classes all of your variables are. for whatever reason, your df is all factors. I'm not sure how it was imported. try New_DF$Temperature <- as.numeric(New_DF$Temperature). If there are NA's in your set, it will be as.numeric(New_DF$Temperature, na.rm = TRUE) That will convert Temperature into a numeric class, and then run the answer above and it should work – Matt W. Feb 04 '17 at 19:11
  • when I change from factor to numeric all the values change. for example 122 becomes 132. this will change the whole condition. – N.A.K Feb 04 '17 at 19:35
  • can you post a few lines of your df so I can take a look at it? – Matt W. Feb 04 '17 at 19:54
  • I'm not sure how to post the table here. But imagine you have 3 columns, x,y and z and you want to delete the rows which have y value > 27. The problem now is that when I convert from factor to numeric the values change, for example, 27 will become 84. – N.A.K Feb 05 '17 at 01:07
  • 1
    @MattW. you don't convert factors to numeric like that - as.numeric returns the *level* integer. Use `as.numeric(as.character(somefactor))` ! – Spacedman Feb 05 '17 at 13:13
  • 1
    @Spacedman that is true! I used as.numeric(as.character()) and it worked. – N.A.K Feb 05 '17 at 14:38
  • @Spacedman ahh you're right, thanks for that edit. – Matt W. Feb 05 '17 at 16:00