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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:19:48+00:00 2026-05-26T07:19:48+00:00

I have a list of data.frames. A <- list(aa = data.frame(var1 = c(1:3), varb

  • 0

I have a list of data.frames.

A <- list(aa = data.frame(var1 = c(1:3), varb = c(1:3), abu = c(1:3)), 
          bb = data.frame(abu = c(1:3), var1 = c(1:3), vara = c(1:3), varb = c(1:3)), 
          cc = data.frame(varb = c(1:3), g = c(1:3))) 

output

> A
$aa
  var1 varb abu
1    1    1   1
2    2    2   2
3    3    3   3

$bb
  abu var1 vara varb
1   1    1    1    1
2   2    2    2    2
3   3    3    3    3

$cc
  varb g
1    1 1
2    2 2
3    3 3

>

How can I order variables in these data.frames so that the order of the variables is the same as the data.frame $aa has? If the data.frame does not have a correspondign variable, the variable should be created into this data.frame containing NA data. Any idea how to achieve this?

  • 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-26T07:19:48+00:00Added an answer on May 26, 2026 at 7:19 am

    do you want something like this?

    > lapply(A, function(x) {
    +   x[(setdiff(names(A$aa), names(x)))] <- NA
    +   x[names(A$aa)]
    + })
    $aa
      var1 varb abu
    1    1    1   1
    2    2    2   2
    3    3    3   3
    
    $bb
      var1 varb abu
    1    1    1   1
    2    2    2   2
    3    3    3   3
    
    $cc
      var1 varb abu
    1   NA    1  NA
    2   NA    2  NA
    3   NA    3  NA
    

    How do you want to do with the variables that do not appear in A$aa, for example, A$bb$vara?

    Slightly updated for preserving other variables

    > lapply(A, function(x) {
    +   x[setdiff(names(A$aa), names(x))] <- NA
    +   x[c(names(A$aa), setdiff(names(x), names(A$aa)))]
    + })
    $aa
      var1 varb abu
    1    1    1   1
    2    2    2   2
    3    3    3   3
    
    $bb
      var1 varb abu vara
    1    1    1   1    1
    2    2    2   2    2
    3    3    3   3    3
    
    $cc
      var1 varb abu g
    1   NA    1  NA 1
    2   NA    2  NA 2
    3   NA    3  NA 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a list of data frames. Where each data frame has
Possible Duplicate: write.csv() a list of unequally sized data.frames I have a data frame
I have a list of data that looks like the following: // timestep,x_position,y_position 0,4,7
I have a list of data that is a schedule. Each item has a
I have a long list of data that I want to display in table
I have a rather large list of data that contains 5 properties per element.
i have a large list of static data from a server that i load
Suppose I have a function that creates data frames. I'd like to run that
I have list of data.frames and I wonder whether there is an easy indexing
I have some data in a dataframe calvarbyruno.1 with variables Nominal and PAR that

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.