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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:12:57+00:00 2026-05-28T00:12:57+00:00

I would like to know how I solve the following problem using higher order

  • 0

I would like to know how I solve the following problem using higher order functions like ddply, ldply, dlply, and avoid using problematic for loops.

The problem:
I have a .csv file representing a dataset loaded into a data.frame, with each row containing the path to a directory where more information is stored in files. I want to use the directory information in the datas.frame to open the files(“file1.txt”,”file2.txt”) in that directory, merge them, then combine the merged files from each entry in one large dataframe.
something like this:
df =

entryName,dir
1,/home/guest/data/entry1
2,/home/guest/data/entry2
3,/home/guest/data/entry3
4,/home/guest/data/entry4

what I would like to do is apply a function to the dataframe that take the directory,
appends a couple of file names “file1.txt”, “file.txt”, then merges the two files together based off a given field.

for example file1.txt could be:

entry,subEntry,value
1,A,2
1,B,3
1,C,4
1,D,5
1,E,3
1,F,3

for example file2.txt could be:

entry,subEntry,value
1,A,8
1,B,7
1,C,8
1,D,9
1,E,8
1,F,7

the output would look something like this:

entryName,subEntry,valueFromFile1,valueFromFile2
1,A,2,8
1,B,3,7
1,C,4,8
1,D,5,9
1,E,3,8
1,F,3,7
2,A,4,8
2,B,5,9
2,C,6,7
2,D,3,7
2,E,6,8
2,F,5,9

Right now I am using a for loop, but for obvious reasons would like to use a higher order function. Here is what I have so far:

allCombined <- data.frame()
df <- read.csv(file="allDataEntries.csv",header=true) 
numberOfEntries = <- dim(df)[1]

for(i in 1:numberOfEntries){ 
  dir <- df$dir[i]
  file1String <- paste(dir,"/file1.txt",sep='') 
  file2String <- paste(dir,"/file2.txt",sep='')
  file1.df <- read.csv(file=file1String,header=TRUE)
  file2.df <- read.csv(file=file2String,header=TRUE)
  localMerged <- merge(file1.df,file2.df, by="value")
  allCombined <- rbind(allCombined,localMerged) 
} 
#rest of my analysis...
  • 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-28T00:12:58+00:00Added an answer on May 28, 2026 at 12:12 am

    Here is one way to do it. The idea is to create a list with contents of all the files, and then use Reduce to merge them sequentially using the common columns entry and subEntry.

    # READ DIRECTORIES, FILES AND ENTRIES
    dirs    <- read.csv(file = "allDataEntries.csv", header = TRUE, as.is = TRUE)$dir
    files   <- as.vector(outer(dirs, c('file.txt', 'file2.txt'), 'file.path'))
    entries <- lapply(files, 'read.csv', header = TRUE)
    
    # APPLY CUSTOM MERGE FUNCTION TO COMBINE ENTRIES
    merge_by <- function(x, y){
      merge(x, y, by = c('entry', 'subEntry'))
    }
    Reduce('merge_by', entries)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to solve the following problem using constraints but i actually don't
I would like to know if I can open 2 different diagrams using MS
I would like to know if there is a way to use an order
I know this question has been asked already but I would like to solve
I have the following example code and would like to know what kind of
I am using Ruby on Rails 3.1 and I would like to know how
i would like know some reference. I know i can googling it. but prefer
Would like to know what a programmer should know to become a good at
Would like to know the c# code to actually retrieve the IP type: Static
I would like to know which dependency described in my pom.xml brings a transitive

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.