I would like to subset a dataframe by referring to a column with a string and select the values of that column that fulfill a condition. From the following code
employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
employ.data <- data.frame(employee, salary, startdate)
salary_string <- "salary"
I want to get all salaries over 23000 by using the salary_string to refer to the column name.
I tried without succes:
set <- subset(employ.data, salary_string > 23000)
set2 <- employ.data[, employ.data$salary_string > 23000)
This does not seem to work because the salary_string is of type character but what I need is some sort of "column name object". Using as.name(salary_string) does not work neither. I know I could get the subset by using
set <- subset(employ.data, salary > 23000)
But my goal is to use the column name that is of type character (salary_string) once with subset(employ.data, ... ) and once with employ.data[, ...]