Dplyr select_和starts_with对变量列表第2部分中的多个值 [英] Dplyr select_ and starts_with on multiple values in a variable list part 2

查看:129
本文介绍了Dplyr select_和starts_with对变量列表第2部分中的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我先前问题的延续:

This is a continuation from my question earlier: Dplyr select_ and starts_with on multiple values in a variable list

我正在从不同位置的不同传感器收集数据,数据输出类似于:

I am collecting data from differnt sensors in various locations, data output is something 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))

(我认为)问题类似于:使用select_和starts_with R 或者 使用dplyr基于多个字符串选择列

The problem is (I think) similar to: Using select_ and starts_with R or select columns based on multiple strings with dplyr

我想例如按位置搜索传感器,所以我有一个列表可以搜索数据框,还包括时间戳.但是,当我搜索多个传感器(或传感器类型等)时,搜索就会分崩离析.有没有一种方法可以使用dplyr(NSE或SE)实现这一目标?

I want to search for sensors for example by location so I have a list to search through the dataframe and also include the timestamp. But searching falls apart when I search for more than one sensor (or type of sensor etc). Is there a way of using dplyr (NSE or SE) to achieve this?

FindLocation = c("date", "Sensor1", "Sensor2")
df %>% select(matches(paste(FindLocation, collapse="|"))) # works but picks up "Sensor1a" and "DewPoint" and "Humidity" data from Sensor2 

我还想添加混合搜索,例如:

Also I want to add mixed searches such as:

 FindLocation = c("Sensor1", "Sensor2") # without selecting "Sensor1a"
 FindSensor = c("Temp", "Pressure") # without selecting "DewPoint" or "Humidity"

我希望选择将FindSensor与FindLocation结合在一起,并为Sensor1和Sensor2选择温度和压力数据(而不选择Sensor1a).返回带有数据和列标题的数据框:

I am hoping the select combines FindSensor with FindLocation and selects Temp and Pressure data for Sensor1 and Sensor2 (without selecting Sensor1a). Returning the dataframe with the data and the columns headings:

日期,传感器1温度,传感器1压力,传感器2温度,传感器2压力

date, Sensor1 Temp, Sensor1 Pressure, Sensor2 Temp, Sensor2 Pressure

再次感谢!

推荐答案

purrr中的某些功能将很有用.首先,使用cross2计算FindLocationFindSensor的笛卡尔积.您会得到一个成对的列表.然后,使用map_chrpaste应用于它们,并使用点(.)将位置和传感器字符串连接在一起.然后,使用one_of助手选择列.

Some functions from purrr are going to be useful. First, you use cross2 to compute the cartesian product of FindLocation and FindSensor. You'll get a list of pairs. Then you use map_chr to apply paste to them, joining the location and sensor strings with a dot (.). Then you use the one_of helper to select the colums.

library(purrr)

FindLocation = c("Sensor1", "Sensor2")
FindSensor = c("Temp", "Pressure")

columns = cross2(FindLocation, FindSensor) %>%
  map_chr(paste, collapse = ".")

df %>% select(one_of(columns))

这篇关于Dplyr select_和starts_with对变量列表第2部分中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆