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 6047777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:23:20+00:00 2026-05-23T07:23:20+00:00

Define a list dats with two dataframes, df1 and df2 dats <- list( df1

  • 0

Define a list dats with two dataframes, df1 and df2

dats <- list( df1 = data.frame(a=sample(1:3), b = sample(11:13)),
    df2 = data.frame(a=sample(1:3), b = sample(11:13)))

> dats
$df1
  a  b
1 2 12
2 3 11
3 1 13

$df2
  a  b
1 3 13
2 2 11
3 1 12

I would like to drop variable a in each data frame. Next I would like to add a variable with the id of each dataframe from an external dataframe, like:

ids <- data.frame(id=c("id1","id2"),df=c("df1","df2"))
> ids
  id  df
1 id1 df1
2 id2 df2

To drop unnecessary vars I tried this without luck:

> dats <- lapply(dats, function(x) assign(x, x[,c("b")]))  
> Error in assign(x, x[, c("b")]) : invalid first argument

Not sure how to add the id either.

I also tried, perhaps more appropriately:

> temp <- lapply(dats, function(x) subset(x[1], select=x[[1]]$b))
Error in x[[1]]$b : $ operator is invalid for atomic vectors

What I find confusing is that str(out[1]) returns a list, str(out[[1]]) returns a dataframe. I think that may have something to do with it.

  • 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-05-23T07:23:21+00:00Added an answer on May 23, 2026 at 7:23 am

    Or try this: Extract your ids into a named vector that maps the data-frame name to the id:

    df2id <- ids$id
    names(df2id) <- ids$df
    
    > df2id
    df1 df2 
    id1 id2 
    Levels: id1 id2
    

    Then use mapply to both (a) drop the a column from each data-frame, and (b) add the id column:

    > mapply( function(d,x) cbind( subset(d, select = -a),
    +                              id = x),
    +         dats, df2id[ names(dats) ] ,
    +         SIMPLIFY=FALSE)
    $df1
       b  id
    1 12 id1
    2 11 id1
    3 13 id1
    
    $df2
       b  id
    1 12 id2
    2 11 id2
    3 13 id2
    

    Note that we are passing df2id[ names(dats) ] to the mapply — this ensures that the data-frames in df2id are “aligned” with the data-frames in dats.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Define: > dats <- list( df1 = data.frame(a=sample(1:3), b = as.factor(rep(325.049072M,3))), + df2 =
i would like to define a list(List of Bank Customer) from predicate and process
I would like to define a list of functions to use within juxt, however
I would like to make an std::list of arrays. specifically, I want a list
I have to define a List and it has two types of possible values
I have the following items (define itemslist (list 'a1 'b2 'c3 (list 'z1 'z2)
Suppose I have a list of #define s in a header file for an
Is it possible to define a recursive list comprehension in Python? Possibly a simplistic
(define (lcs lst1 lst2) (define (except-last-pair list) (if (pair? (cdr list)) (cons (car list)
I want to define a generator from a list that will output the elements

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.