r - Dplyr select_ and starts_with on multiple values in a variable list part 2 -


this continuation question earlier: dplyr select_ , starts_with on multiple values in variable list

i collecting data differnt sensors in various locations, data output like:

df<-data.frame(date=c(2011,2012,2013,2014,2015),"sensor1 temp"=c(15,18,15,14,19),"sensor1 pressure"=c(1001, 1000, 1002, 1004, 1000),"sensor1a temp"=c(15,18,15,14,19),"sensor1a pressure"=c(1001, 1000, 1002, 1004, 1000), "sensor2 temp"=c(15,18,15,14,19),"sensor2 pressure"=c(1001, 1000, 1002, 1004, 1000), "sensor2 dewpoint"=c(10,11,10,9,12),"sensor2 humidity"=c(90, 100, 90, 100, 80)) 

the problem (i think) similar to: using select_ , starts_with r or select columns based on multiple strings dplyr

i want search sensors example location have list search through dataframe , include timestamp. searching falls apart when search more 1 sensor (or type of sensor etc). there way of using dplyr (nse or se) achieve this?

findlocation = c("date", "sensor1", "sensor2") df %>% select(matches(paste(findlocation, collapse="|"))) # works picks "sensor1a" , "dewpoint" , "humidity" data sensor2  

also want add mixed searches such as:

 findlocation = c("sensor1", "sensor2") # without selecting "sensor1a"  findsensor = c("temp", "pressure") # without selecting "dewpoint" or "humidity" 

i hoping select combines findsensor findlocation , selects temp , pressure data sensor1 , sensor2 (without selecting sensor1a). returning dataframe data , columns headings:

date, sensor1 temp, sensor1 pressure, sensor2 temp, sensor2 pressure

many again!

some functions purrr going useful. first, use cross2 compute cartesian product of findlocation , findsensor. you'll list of pairs. use map_chr apply paste them, joining location , sensor strings dot (.). use one_of helper select colums.

library(purrr)  findlocation = c("sensor1", "sensor2") findsensor = c("temp", "pressure")  columns = cross2(findlocation, findsensor) %>%   map_chr(paste, collapse = ".")  df %>% select(one_of(columns)) 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -