Error in if : missing value where TRUE/FALSE needed

I have a data frame called Data that I want to analyse with some basic statistics such as ACP.
But I found that inside the variable Land use change there is some negative values.
So my goal is to use only variables without those negatives.
I tried to implement a loop to remove rows that contains negatives values of Land use change.
When I try to run it into the console, it works but I can't "knit" the markdown document :

# Removing variables with negatives values for Land use change

for(i in 1:length(Data$Retail)) {
  if(Data$`Land use change`[i]<0) {
    Data <- Data[-c(i), ]
    }
} 

It returns me this error :

Error in if (Data$`Land use change`[i] < 0) { : 
  missing value where TRUE/FALSE needed

I tried to look for informations in other forums, and I found that this error returns when there is a NA value in the if statement. But as you can see, I don't have any missing value inside the Land use change column.

Any help is welcome !

a lesson on dplyr filter:
5 Data transformation | R for Data Science (had.co.nz)

library(dplyr)

Data <- Data %>% 
           filter(`Land use change` >= 0 )
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.