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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:51:31+00:00 2026-06-06T20:51:31+00:00

Currently my R loop (see below) overwrites itself during each iteration. I want to

  • 0

Currently my R loop (see below) overwrites itself during each iteration. I want to output the result from each loop into a text file.

In more detail:
R beginner here wondering how to include a line in my script so that the mean value calculating for each file is written to a text file. So that the script creates a new empty text file and then each time the script calculates the mean of column 4 in a file that value is output to a row in the text file which contains the name of the file in column1 and the mean value in column 2.
Thanks for your help!

filename <- system("ls /dir/",intern=TRUE)

for(i in 1:length(filename)){

file <- read.table(filename[i],header=FALSE) ## if you have headers in your files ##
mean <- mean(as.numeric(file$V4))



}
  • 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-06T20:51:32+00:00Added an answer on June 6, 2026 at 8:51 pm

    To do it as the loop is running, in your loop add:

    write.csv(data.frame(fname=filename[i],mean=mean),file="output.csv",append=TRUE)
    

    However, this would mean a lot of file system overhead, and it would be quicker the produce the whole data frame in R and then write the file as a whole. So instead of your loop write:

    means <- sapply(filename, function(x) mean(as.numeric(read.table(x,header=FALSE)$V4)))
    

    And then write the file as a whole with:

    write.csv(data.frame(fname=filename,mean=means),file="output.csv")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the code below is from an assignment in my text book for a simple
I currently have a json object, that I loop through and output a list
I am trying to add the results from the while loop into an xml
This questions relating to infowindow in the google maps API v3.. Currently I loop
Currently I have a loop that tries to find an unused filename by adding
Currently I am using IntentService to loop urls in the Browser.apk. I am running
Currently our code uses a for-loop for filling a buffer holding a Unicode string
Currently, I have a while loop for listening the port. When a socket accepta
I am currently iterating through a loop and redimensionalising my array every time I
I have an NSArray of double objects.... I currently I have a for loop

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.