I am looking to filter a data frame based on two criteria: 1. few key variables and, 2. Several other variables with a specific pattern.

Usually I use the following code:

mydata <- rawData[,c("scenario","period","farmID",grep("planting_*",names(rawData), value = TRUE))]

The problem is happening after I updated R to the new version (3.5.3). Before it worked fine and I got what I wanted: a new data frame filtered by variables.

However, now R is returning a vector only with the variable names (with no observation).

Could anyone help me with this issue?

Many thanks!

1 Answers

Konrad Rudolph On

This code never worked: You can’t mix names and numeric indices in the same subsetting vector.

Furthermore, your regular expression is wrong: * should be .* — otherwise you’d be searching for the word planting followed by zero or more underscores. In your case this won’t make a difference but it’s a different expression and thus misleading to readers of your code.

However, you can make it work by making grep return the matched names instead of indices:

planting_names <- grep("planting_.*", names(rawData), value = TRUE)
mydata <- rawData[ , c("scenario", "period", "farmID", planting_names)]