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

Las*_*apa 7 sql r

我有一个数据集分配给一个名为'temps'的变量,它有'date','temperature','country'列.
我想做这样的事情,我可以在SQL中做

SELECT * FROM temps WHERE country != 'mycountry'
Run Code Online (Sandbox Code Playgroud)

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

akr*_*run 10

我们可以使用类似的语法 base R

temps[temps$country != "mycountry",]
Run Code Online (Sandbox Code Playgroud)

基准

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 
Run Code Online (Sandbox Code Playgroud)

如果我们使用包解决方案

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 
Run Code Online (Sandbox Code Playgroud)


mil*_*lan 5

这应该这样做。

temps2 <- temps[!temps$country %in% "mycountry",]
Run Code Online (Sandbox Code Playgroud)