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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:10:20+00:00 2026-06-16T02:10:20+00:00

I am trying to build a database in R from multiple csvs. There are

  • 0

I am trying to build a database in R from multiple csvs. There are NAs spread throughout each csv, and I want to build a master list that summarizes all of the csvs in a single database. Here is some quick code that illustrates my problem (most csvs actually have 1000s of entries, and I would like to automate this process):

d1=data.frame(common=letters[1:5],species=paste(LETTERS[1:5],letters[1:5],sep='.'))
d1$species[1]=NA
d1$common[2]=NA
d2=data.frame(common=letters[1:5],id=1:5)
d2$id[3]=NA
d3=data.frame(species=paste(LETTERS[1:5],letters[1:5],sep='.'),id=1:5)

I have been going around in circles (writing loops), trying to use merge and reshape(melt/cast) without much luck, in an effort to succinctly summarize the information available. This seems very basic but I can’t figure out a good way to do it. Thanks in advance.

To be clear, I am aiming for a final database like this:
  common species id
1      a     A.a  1
2      b     B.b  2
3      c     C.c  3
4      d     D.d  4
5      e     E.e  5
  • 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-16T02:10:21+00:00Added an answer on June 16, 2026 at 2:10 am

    I recently had a similar situation. Below will go through all the variables and return the most possible information to add back in to the dataset. Once all data is there, running one last time on the first variable will give you the result.

    #combine all into one dataframe
    require(gtools)
    d <- smartbind(d1,d2,d3)
    
    #function to get the first non NA result
    getfirstnonna <- function(x){
      ret <- head(x[which(!is.na(x))],1)
      ret <- ifelse(is.null(ret),NA,ret)
      return(ret)
    }
    
    #function to get max info based on one variable
    runiteration <- function(dataset,variable){
      require(plyr)
      e <- ddply(.data=dataset,.variables=variable,.fun=function(x){apply(X=x,MARGIN=2,FUN=getfirstnonna)})
      #returns the above without the NA "factor"
      return(e[which(!is.na(e[ ,variable])), ])
    }
    
    #run through all variables
    for(i in 1:length(names(d))){
      d <- rbind(d,runiteration(d,names(d)[i]))
    }
    #repeat first variable since all possible info should be available in dataset
    d <- runiteration(d,names(d)[1])
    

    If id, species, etc. differ in separate datasets, then this will return whichever non-NA data is on top. In that case, changing the row order in d, and changing the variable order could affect the result. Changing the getfirstnonna function will alter this behavior (tail would pick last, maybe even get all possibilities). You could order the dataset by the most complete records to the least.

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

Sidebar

Related Questions

I am trying to build a hierarchical array in PHP, from relational database contents
I'm trying to build a search for our internal support database - each support
i'm new to asp.net and i'm trying to build a treeview from database for
I am currently trying to build a cakephp app that will list items from
I'm trying to build a graph in achartengine with data from my SQLite database.
I'm trying to build something that will randomly pick two objects from a list
Trying to build a ajax comment system .I have a list populated from MYSQL
I am trying to build a database project, and then use the output along
I'm trying to build out a mysql database design for a project. The problem
I am trying to build a Python application that connects to Oracle database and

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.