SQL查询列表中的数据帧

dmv*_*nna 4 r list dataframe sqldf data.table

给定数据帧

df1 <- data.frame(CustomerId=c(1:6),Product=c(rep("Toaster",3),rep("Radio",3)))
df2 <- data.frame(CustomerId=c(2,4,6),State=c(rep("Alabama",2),rep("Ohio",1)))
Run Code Online (Sandbox Code Playgroud)

存储在列表中

dflist <- c(df1,df2)
Run Code Online (Sandbox Code Playgroud)

如何在这些数据帧上运行sqldf查询(连接)?

尝试失败:

test <- sqldf("select a.CustomerId, a.Product, b.State from dflist[1] a
          inner join dflist[2] b on b.id = a.id")

test <- sqldf("select a.CustomerId, a.Product, b.State from dflist$df1 a
          inner join dflist$df2 b on b.CustomerId = a.CustomerId")
Run Code Online (Sandbox Code Playgroud)

mne*_*nel 11

如果将data.frames从列表复制到新环境,则可以使用envir参数sqldf或命名列表元素并使用with.

请注意以下几点:

  • 我创建dflist使用listc.

注意区别

str(c(df1,df2))
##List of 4
## $ CustomerId: int [1:6] 1 2 3 4 5 6
## $ Product   : Factor w/ 2 levels "Radio","Toaster": 2 2 2 1 1 1
## $ CustomerId: num [1:3] 2 4 6
## $ State     : Factor w/ 2 levels "Alabama","Ohio": 1 1 2

str(list(df1,df2))
##List of 2
## $ :'data.frame': 6 obs. of  2 variables:
##  ..$ CustomerId: int [1:6] 1 2 3 4 5 6
##  ..$ Product   : Factor w/ 2 levels "Radio","Toaster": 2 2 2 1 1 1
## $ :'data.frame': 3 obs. of  2 variables:
##  ..$ CustomerId: num [1:3] 2 4 6
##  ..$ State     : Factor w/ 2 levels "Alabama","Ohio": 1 1 2
Run Code Online (Sandbox Code Playgroud)
  • 我调整了sql查询以反映data.frames中的名称(根据你的第二种方法)

命名数据

dflist <- list(df1,df2)
names(dflist) <- c('df1','df2')
Run Code Online (Sandbox Code Playgroud)

创建一个新的工作环境

# create a new environment

e <- new.env()
# assign the elements of dflist to this new environment
for(.x in names(dflist)){
  assign(value = dflist[[.x]], x=.x, envir = e)
}

# this could also be done using mapply / lapply
# eg
# invisible(mapply(assign, value = dflist, x = names(dflist), MoreArgs =list(envir = e)))
# run the sql query
sqldf("select a.CustomerId, a.Product, b.State from df1 a
          inner join df2 b on b.CustomerId = a.CustomerId", envir = e)

##  CustomerId Product   State
## 1          2 Toaster Alabama
## 2          4   Radio Alabama
## 3          6   Radio    Ohio
Run Code Online (Sandbox Code Playgroud)

一种更简单的方法 with

你可以简单地使用with哪个本地评估(重要的是dflist是一个命名列表)

# this is far simpler!!
with(dflist,sqldf("select a.CustomerId, a.Product, b.State from df1 a
           inner join df2 b on b.CustomerId = a.CustomerId"))
Run Code Online (Sandbox Code Playgroud)

使用的另一个简单方法 proto

  • 感谢@ G.Grothendieck(见评论

这使用了proto加载的包sqldf

dflist <- list(a = df1, b = df2)
sqldf( "select a.CustomerId, a.Product, b.State from df1 a 
         inner join df2 b on b.CustomerId = a.CustomerId", 
         envir = as.proto(dflist))
Run Code Online (Sandbox Code Playgroud)

使用data.table

或者您可以使用data.table哪种sql-like方法(参见FAQ 2.16)

library(data.table)
dflist <- list(data.table(df1),data.table(df2))
names(dflist) <- c('df1','df2')
invisible(lapply(dflist, setkeyv, 'CustomerId'))
with(dflist, df1[df2])
##    CustomerId Product   State
## 1:          2 Toaster Alabama
## 2:          4   Radio Alabama
## 3:          6   Radio    Ohio
Run Code Online (Sandbox Code Playgroud)