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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:02:50+00:00 2026-06-17T17:02:50+00:00

A question that is undoubtedly easy to solve for an R expert. I need

  • 0

A question that is undoubtedly easy to solve for an R expert.

I need to repeat a number of functions on dataframes that are sequentially labeled (before merging them all together). For example, I might need to do the following:

# READ IN DATAFILES & LABEL DF'S 
df1 <- read.csv(file="file_A.csv",head=TRUE) 
df2 <- read.csv(file="file_B.csv",head=TRUE) 
df3 <- read.csv(file="file_C.csv",head=TRUE)

# TURN DF'S INTO DATA TABLES
df1<-data.table(df1)
df2<-data.table(df2)
df3<-data.table(df3)

# CHANGE VARIABLE TO POSIX
df1$date <-as.POSIXct(df1$date, format = "%Y-%m-%d %H:%M:%S")
df2$date <-as.POSIXct(df2$date, format = "%Y-%m-%d %H:%M:%S")
df3$date <-as.POSIXct(df3$date, format = "%Y-%m-%d %H:%M:%S")

# FILTER BY DATE RANGE
date_filter<-as.POSIXct("2012-01-01 01:01:01")
df1<-subset(df1, df1$date>date_filter)
df2<-subset(df2, df2$date>date_filter)
df3<-subset(df3, df3$date>date_filter)

# AGGREGATE OVER A UNIQUE ID 
df1<-df1[,(sum(var)), by=list(id)] 
df2<-df2[,(sum(var)), by=list(id)] 
df2<-df2[,(sum(var)), by=list(id)] 

# FINALLY, MERGE TOGETHER
df <-merge(df1,df2, by="id",all=TRUE)

You get the idea–only I need to do this for 25 dataframes, not 3. I have a suspicion that I can make R repeat functions by creating a vector (df_nums<-c(1:25))) and then using a function to loop over all of my data frames, but I don’t know how to do it.

Please help! Thanks!

Edit: Thanks to Arun, I’m up to this for my actual code:

out<- lapply(1:length(files), function(idx) {
  df <- as.data.table(read.csv(files[idx], header = TRUE))
  df$date <- as.POSIXct(df$date, format = "%Y-%m-%d %H:%M:%S")
  date_filter <- as.POSIXct("2012-11-13 01:01:01")
  df <- subset(df, df$date > date_filter)
  df <- df[, .N, by = list(id)] 
})
out<-data.table(out)
out.merge <- Reduce(function(...) merge(..., by="id", all=T), out)

Edit 2: After running the following syntax, I appear to have data.tables nested in out. For example,

> head(out)
            out
1: <data.table>
2: <data.table>
3: <data.table>
4: <data.table>
5: <data.table>
6: <data.table>

How do I access these data.tables to see if everything worked correctly?

  • 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-17T17:02:51+00:00Added an answer on June 17, 2026 at 5:02 pm

    You can use list.files to obtain all the CSV files from the directory and use lapply to recurse, in this manner:

    # Thanks Matthew for correcting the pattern string
    files <- list.files("path_to_files", full.names = TRUE, pattern="\\.csv$") 
    out <- lapply(1:length(files), function(idx) {
        df <- as.data.table(read.csv(files[idx], header = TRUE))
        df$date <- as.POSIXct(df$date, format = "%Y-%m-%d %H:%M:%S")
        date_filter <- as.POSIXct("2012-01-01 01:01:01")
        df <- subset(df, df$date > date_filter)
        df <-df[, (sum(var)), by = list(id)]
    })
    

    You can use do.call(rbind, out) or do.call(cbind, out) to bind all results by row or columns.

    Edit: After @roody’s question about outer join. Something like this?

    out.merge <- Reduce(function(...) merge(..., by="id", all=T), out)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL question that i need to ask the experts. To be
I have a question that should be quick and easy for you guys to
This question will undoubtedly be difficult to answer and ask in a way that
This is undoubtedly a simple question. I used to do this before, but it's
After much searching and looking extensively, I have a question that is undoubtedly dumb
This is probably a question that has an easy/simple/obvious answer, but I've found myself
A simple question that could remove the need for quite a lot of if/else
I have a question that must be answer several times before, but I can't
This may be a question that has been answered before - if so please
A question that seems to have quite a few options for Python, but none

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.