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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:15:42+00:00 2026-05-31T11:15:42+00:00

I have a folder with 20 text files from several instrument runs. The data

  • 0

I have a folder with 20 text files from several instrument runs. The data section all have the same format like this

22/05/11;   16:03:28;       0.000;   6.079;   31.41;   84881;   25.60; E0;
22/05/11;   16:03:29;       0.017;   6.063;   31.44;   84868;   25.60; E0;
22/05/11;   16:03:30;       0.034;   6.079;   31.41;   84868;   25.60; E0;
22/05/11;   16:03:31;       0.051;   6.079;   31.41;   84868;   25.60; E0;
22/05/11;   16:03:32;       0.068;   6.068;   31.43;   84868;   25.60; E0;
22/05/11;   16:03:33;       0.085;   6.068;   31.43;   84881;   25.60; E0;
22/05/11;   16:03:34;       0.102;   6.079;   31.41;   84874;   25.60; E0;

What I would like to do is read in every file within my folder, run a linear regression, and pull out the slope and R2 value.

As of now, this is my code for doing this for a single file.

O2=read.table("Coral 1_Dark.txt",skip=58, sep=";",header=FALSE)
names(O2)<-c("Date","Time","Log_Time","O2_mgL","Phase","Amp","Temp C","Error Message")
O2$id<-seq_len(nrow(O2)) #creates unique ID for each measurement (use for regression)
attach(O2)
fit=lm(O2_mgL~id)
summary(fit)

After running this code, I have been manually entering the slope and R2 data.

For now I can create a variable that contains all the files I am interested in with

F=list.files()

Which gives me all 20 files

[1] "Coral 1_Dark.txt"   "Coral 1_Light.txt"  "Coral 10_Dark.txt"  "Coral 10_Light.txt" "Coral   2_Dark.txt"  
[6] "Coral 2_Light.txt"  "Coral 3_Dark.txt"   "Coral 3_Light.txt"  "Coral 4_Dark.txt"   "Coral  4_Light.txt" 
[11] "Coral 5_Dark.txt"   "Coral 5_Light.txt"  "Coral 6_Dark.txt"   "Coral 6_Light.txt"  "Coral 7_Dark.txt"  
[16] "Coral 7_Light.txt"  "Coral 8_Dark.txt"   "Coral 8_Light.txt"  "Coral 9_Dark.txt"   "Coral 9_Light.txt" 

And what I would like to end up with would be something like this for all 20 files

Coral            Slope        R2
Coral 1_Dark      0.23         98.3
Coral 2_Dark      0.33         99.3

ect

Any suggestions? I’ve never worked with any of the apply functions or any kind of loop- but I’m thinking this is about to change…..

  • 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-31T11:15:43+00:00Added an answer on May 31, 2026 at 11:15 am

    Something like this?

    wd <- "C:/Data"
    files    <- dir(wd)
    varnames <- c("Date", "Time", "Log_Time", "O2_mgL", "Phase", "Amp", "Temp C",
                "Error Message")
    results  <- data.frame()
    
    for (i in 1:length(files)) {
      fname <- paste(wd, files[i], sep="/")
      data <- read.table(fname, sep=";", skip=58)
      colnames(data) <- varnames
      data$id <- 1:nrow(data)
      fit <- summary(lm(O2_mgL~id, data=data))
      results[i,1] <- fit$coefficients[2]
      results[i,2] <- fit$r.squared
    }
    
    rownames(results) <- sub(".txt", "", files)
    colnames(results) <- c("Slope", "R2")
    
    print(results)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a folder with thousands of text files and I'd like to paste
I have many text files in a folder. What I can do now is
I have a folder with 10 text files at C:\TEXTFILES\ drive in my machine.
I have several hundred files in a folder. Each of these file is a
In my app i have one use case Download Folder(s) from web server this
I have lost files mysteriously a few times from my Eclipse workspace folder, and
I need to export all of the objects from my MDB to text files
I have some text files in a folder named foo1, foo2,...,foo5. I tried to
I have several csv files that I need to transfer over to mdb format.
I have folder home/admin. In this folder there is index.php. When i access to

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.