简体   繁体   中英

How to query and display values from a data frame column based on values in a separate vector

I am attempting to query values in one column of a data frame and would like to return a series rows in this data frame based on similar values of a separate vector that I specify in the query. This is basically a subset, but based on values not necessarily included in the data frame.

In my data frame shown here:

temperature <- c(30:40)
gas_price <- c(1:11)
date <- as.Date(c('2010-11-1','2008-3-25','2007-3-14','2015-01-12',
'2012-04-15','2013-09-17','2009-06-26','2011-10-12','2001-07-04','2012-12-25','2015-02-08'))

gas.data <- data.frame(temperature, gas_price, date)

I have been attempting to query the temperature column based on a weather forecast vector that we'll call x

x  <- c(32,39,37,31,36)

Given that x is a 5 day forecast, I've been attempting to query gas.data$temperature based on certain values of x. I would like to have rows of the gas.data data frame returned with the same gas.data$temperature number as the x query.

x[x[1] %in% gas.data$temperature]  

doesn't work nor do my attempts with which or grepRaw

I am also looking to potentially return a range of values in gas.data$temperature based on a specific value query from x, but not sure how to go about that either. I'm assuming it would be similar to the solution above, but could include the range function.

Any ideas? Thanks

没有示例输出,我不确定您要查找的是什么,但是以下代码片段为您提供了gas.data中与x中的温度相对应的行:

gas.data[gas.data$temperature %in% x,]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM