Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:11:01+00:00 2026-06-11T03:11:01+00:00

Given the dataframes 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))) are stored in a list

  • 0

Given the dataframes

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)))

are stored in a list

dflist <- c(df1,df2)

how do I run sqldf queries (joins) on these dataframes?

Failed attempts:

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")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:11:03+00:00Added an answer on June 11, 2026 at 3:11 am

    If you copy your data.frames from the list to a new environment, then you can use the envir argument to sqldf or by naming the elements of the list, and using with.

    Note a couple of things:

    • I create dflist using list not c.

    note the difference

    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
    
    • I have adjusted the sql queries to reflect the names within the data.frames (as per your second approach)

    the named data

    dflist <- list(df1,df2)
    names(dflist) <- c('df1','df2')
    

    Create a new environment to work in

    # 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
    

    A simpler approach using with

    you could simply use with which evalulates locally (important that dflist is a named list here)

    # 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"))
    

    Another simple approach using proto

    • Thanks to @G.Grothendieck (see the comments

    This uses the proto package which is loaded with 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))
    

    Using data.table

    Or you could use data.table which gives sql-like approaches (see 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two data frames: df1 = data.frame(CustomerId = c(1:6), Product = c(rep(Toaster, 3), rep(Radio,
Given two data frames: C1<-c(3,4,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,6,3,7,4) DF<-data.frame(C1=C1,C2=C2,C3=C3) DF C1 C2 C3 1 3
I have a dataframe: > df <- data.frame( + Species = rep(LETTERS[1:4], times=c(5,6,7,6)), +
Given a data frame, DF, it is simple to save DF as an R
Given a MySQL table of real estate data, I would like to generate a
Given 2 data frames that are identical in terms of column names/datatypes, where some
Possible Duplicate: Get column index from label in a data frame I need to
Suppose I have the following dataframe: df <- data.frame(BR.a=rnorm(10), BR.b=rnorm(10), BR.c=rnorm(10), USA.a=rnorm(10), USA.b =
Say I have a data frame like this: Df <- data.frame( V1 = c(1,2,3,NA,5),
How can I evaluate a string of class character as data frame? Concretely, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.