all_trips_v2 <- all_trips[!(all_trips$start_station_name == "HQ QR" | all_trips$ride_length<0),]
What does !
do in this situation? Can anyone explain this?
CodePudding user response:
From documentation ! indicates logical negation (NOT). To get help use:
?`!`
In your example select rows that does not have value start_station_name
equal to HQ HR
or negative ride length
CodePudding user response:
In this case, the symbol !
is the same as NOT. That is, in your example
all_trips[!(all_trips$start_station_name == "HQ QR" | all_trips$ride_length<0),]
what you are doing can be explained in two steps:
- In
(all_trips$start_station_name == "HQ QR" | all_trips$ride_length<0)
, you want to know if the elements of the column start_station_name are equal to "HQ QR" OR if the elements of the column ride_length are smaller than 0. - By doing
all_trips[!(all_trips$start_station_name == "HQ QR" | all_trips$ride_length<0),]
, you are subsetting the rows in all_trips dataframe that DO NOT satisfy the previous conditions.
Hope this helps!