与R中的SQL’WHERE’子句类似的函数

我有一个分配给名为’temps’的变量的数据集,它有’date’,’temperature’,’country’列.
我想做这样的事情,我可以在sql中做
SELECT * FROM temps WHERE country != 'mycountry'

如何在R中进行类似的选择?

解决方法

我们可以在基R中使用类似的语法
temps[temps$country != "mycountry",]

基准

set.seed(24)
temps1 <- data.frame(country = sample(LETTERS,1e7,replace=TRUE),val = rnorm(1e7))
system.time(temps1[!temps1$country %in% "A",])
#  user  system elapsed 
#   0.92    0.11    1.04 
system.time(temps1[temps1$country != "A",])
#   user  system elapsed 
#   0.70    0.17    0.88

如果我们使用包解决方

library(sqldf)
system.time(sqldf("SELECT * FROM temps1 WHERE country != 'A'"))
#   user  system elapsed 
# 12.78    0.37   13.15 

library(data.table)
system.time(setDT(temps1,key = 'country')[!("A")])
#   user  system elapsed 
#  0.62    0.19    0.37

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...