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

  • Home
  • SEARCH
  • 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 7063977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:46:40+00:00 2026-05-28T04:46:40+00:00

I have to read some external files, extract some columns and complete the missing

  • 0

I have to read some external files, extract some columns and complete the missing values with zeros. So if the first file has in the column$Name: a, b, c, d, and the column$Area with discrete values; the second file has in the some column: b, d, e, f and so on for the further files I need to create a data frame such this:

        a      b      c      d      e   f
File1 value  value  value  value    0   0
File2   0    value    0    value  value  value

This is the dummy code I wrote to try to better explain my problem:

listDFs <- list()
for(i in 1:10){
    listDFs[[i]] <-
        data.frame(Name=c(
                   c(paste(sample(letters,size=2,replace=TRUE),collapse="")),
                   c(paste(sample(letters,size=2,replace=TRUE),collapse="")),
                   c(paste(sample(letters,size=2,replace=TRUE),collapse="")),
                   c(paste(sample(letters,size=2,replace=TRUE),collapse="")),
                   c(paste(sample(letters,size=2,replace=TRUE),collapse="")),
                   c(paste(sample(letters,size=2,replace=TRUE),collapse="")),
                   c(paste(sample(letters,size=2,replace=TRUE),collapse=""))),
                   Area=runif(7))
}
lComposti <- sapply(listDFs, FUN = "[","Name")
dfComposti <- data.frame(matrix(unlist(lComposti),byrow=TRUE))
colnames(dfComposti) <- "Name"
dfComposti <- unique(dfComposti)
                                        #
## The CORE of the code
lArea <- list()
for(i in 1:10){
    lArea[[i]] <-
        ifelse(dfComposti$Name %in% listDFs[[i]]$Name, listDFs[[i]]$Area, 0)}
                                        #
mtxArea <- (matrix(unlist(lArea),nrow=c(10),ncol=dim(dfComposti)[1],byrow=TRUE))

The problem is about the “synchronization” between the column name and each values.

Have you some suggestion??

If my code result to be un-clear I can also upload the files I work with.

Best

  • 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-28T04:46:41+00:00Added an answer on May 28, 2026 at 4:46 am

    The safest is never to lose track of the names: they could be put back in the wrong order…

    You can concatenate all your data.frames into a tall data.frame, with do.call(rbind, ...), and then convert it to a wide data.frame with dcast.

    # Add a File column to the data.frames
    names( listDFs ) <- paste( "File", 1:length(listDFs) )
    for(i in seq_along(listDFs)) {
      listDFs[[i]] <- data.frame( listDFs[[i]], file = names(listDFs)[i] )
    }
    
    # Concatenate them
    d <- do.call( rbind, listDFs )
    
    # Convert this tall data.frame to a wide one
    # ("sum" is only needed if some names appear several times 
    # in the same file: since you used "replace=TRUE" for the 
    # sample data, it is likely to happen)
    library(reshape2)
    d <- do.call( rbind, listDFs )
    d <- dcast( d, file ~ Name, sum, value.var="Area" )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an external file: path_to_external_file.rb with some class definition: class A some_definitions end
I'm trying to read use fread to read in values from an external file
I have read some posts on here about not mixing parameters when passing into
I have read some posts about this topic and the answers are comet, reverse
I have read some articles on POCO in the enttity framework but still don't
I have just switched from svn to mercurial and have read some tutorials about
I have read in some of the ClickOnce posts that ClickOnce does not allow
I have read from some article that say's Apple doesn't approve the application which
I just got Delphi 2009 and have previously read some articles about modifications that
I am new to ASP.net MVC architecture. I have read in some articles 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.