NBE
NBE

Reputation: 651

R: Using dplyr to filter out a dataframe

I'm new to R and I'm having trouble filtering out my dataframe for a specific condition. For some reason, the code is working and I'm not getting any errors but when I view the updated dataframe... the condition I set didn't excute.

The condition that isn't excuting is the var>50. Any help would be greatly appreciated!

Code so far:

if (!require(pacman)) {
  install.packages('pacman')

}

pacman::p_load("ggplot2", "tidyr", "plyr", "dplyr")
#### Read in the necessary data ######
roadsalt_data <- read.table("QADportaldata_1988-2015.tsv", header = T, sep = "\t", fill = T, stringsAsFactors = F)
# Convert date column from a character class to a date class so ggplot can  display as a continuous variable ###
roadsalt_data$stdate <- as.Date(roadsalt_data$stdate)
## Filter dataset to only contain columns I need ########
filtered_roadsalt <- roadsalt_data %>% 
  select(orgid, stdate, locid, charnam, val) %>%
  filter(between(stdate, as.Date("1996-01-01"), as.Date("2015-07-01"))) %>%
  filter(charnam == "Total dissolved solids" & "var" > 50) 

Preview of my dataset:

'data.frame':   47850 obs. of  5 variables:
 $ orgid  : chr  "USGS-NJ" "USGS-NJ" "USGS-NJ" "USGS-NJ" ...
 $ stdate : Date, format: "2014-03-05" "2014-03-05" "2014-03-04" ...
 $ locid  : chr  "USGS-01367785" "USGS-01367785" "USGS-01455099" "USGS-01455099" ...
 $ charnam: chr  "Total dissolved solids" "Total dissolved solids" "Total dissolved solids" "Total dissolved solids" ...
 $ val    : chr  "0.21" "154" "0.43" "333" ...

Upvotes: 0

Views: 452

Answers (2)

krpa
krpa

Reputation: 104

I am assuming class(val) is a factor, then condition in filter has to be this way:

filter(charnam == "Total dissolved solids" & as.numeric(as.character(val)) > 50.00)

Upvotes: 2

B. Sharp
B. Sharp

Reputation: 178

When using dplyr functions, you don't need quotes around your variable names. So,

filter(charnam == "Total dissolved solids" & "var" > 50) 

should be replaced with

filter(charnam == "Total dissolved solids" & var > 50)

Var also has to be converted to a numeric variable.

That being said, if you select at the beginning of your pipe, you have to include all the variables on which you want to add filters. Because you haven't selected a variable called "var" in your initial select statement, so you won't be able to filter on var. If that's meant to be 'val', then you're good to go.

Upvotes: 2

Related Questions