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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:40:32+00:00 2026-06-15T12:40:32+00:00

I’m having a simple issue that i couldn’t solve with the for loop. I

  • 0

I’m having a simple issue that i couldn’t solve with the for loop.
I calculate and save the mean of a column each time inside my for loop.

In the first loop, the mean is saved to the variable. but in the second loop the mean of column2 will replace the mean of column1. This is a very basic question but I have no idea how to do that.

#read.table(file1)
for (x in 1:100){
f <- mean(file1[,x])
}

I want to save the mean values for all columns in 1 variable (assume it is called “f”).

f <- c(meancol1, meancol2, meancol3... meancol100)

Any ideas?

  • 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-15T12:40:34+00:00Added an answer on June 15, 2026 at 12:40 pm

    Even simpler you could use colMeans function. Here’s an example

    > set.seed(001)  # Generating some random data
    > file1 <- data.frame(matrix(rnorm(50, 100, 5), ncol=5))
    > file1 # this is how the artificial data.frame should look like
              X1        X2        X3        X4        X5
    1   96.86773 107.55891 104.59489 106.79340  99.17738
    2  100.91822 101.94922 103.91068  99.48606  98.73319
    3   95.82186  96.89380 100.37282 101.93836 103.48482
    4  107.97640  88.92650  90.05324  99.73097 102.78332
    5  101.64754 105.62465 103.09913  93.11470  96.55622
    6   95.89766  99.77533  99.71936  97.92503  96.46252
    7  102.43715  99.91905  99.22102  98.02855 101.82291
    8  103.69162 104.71918  92.64624  99.70343 103.84266
    9  102.87891 104.10611  97.60925 105.50013  99.43827
    10  98.47306 102.96951 102.08971 103.81588 104.40554
    > colMeans(file1)  # this part computes the means for each column without a 'for' loop
           X1        X2        X3        X4        X5 
    100.66101 101.24422  99.33163 100.60365 100.67068 
    

    Take a look at ?colMeans

    If you have non-numeric columns you can skip them autotically using sapply inside colMeans for example:

    set.seed(001)  # Generating some random data
    file1 <- data.frame(matrix(rnorm(50, 100, 5), ncol=5))
    # Creating three columns with non-numeric data
    factors <- data.frame(matrix(sample(letters, 30, TRUE), ncol=3))
    
    file1 <- cbind(factors, file1) # this is your data.frame
    colnames(file1) <- paste0('Col.', 1:ncol(file1)) # set some colnames
    file1 # this is the data.frame to work with
    > colMeans(file1[sapply(file1, is.numeric)])# colmeans for only those numeric cols
        Col.4     Col.5     Col.6     Col.7     Col.8 
    100.65027 101.52467 102.04486  99.14944 100.23847 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

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.