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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:36:10+00:00 2026-05-14T06:36:10+00:00

I’m trying to add a Row to my data.frame to spit out the average

  • 0

I’m trying to add a Row to my data.frame to spit out the average of the column.

This is my data frame:

              Weight Response
1   Control     59      0.0
2 Treatment     90      0.8
3 Treatment     47      0.1
4 Treamment    106      0.1
5   Control     85      0.7
6 Treatment     73      0.6
7   Control     61      0.2

I’d like it to become:

              Weight Response
1   Control     59      0.0
2 Treatment     90      0.8
3 Treatment     47      0.1
4 Treamment    106      0.1
5   Control     85      0.7
6 Treatment     73      0.6
7   Control     61      0.2
8 AVERAGES      74      0.3

Thanks!

  • 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-14T06:36:10+00:00Added an answer on May 14, 2026 at 6:36 am

    Simple one:

    rbind(data, AVERAGES=colMeans(data))
    

    [Edit] If your data.frame contains other types than numeric (like factor or character) then you could use more complicated but safer method:

    rbind(data, AVERAGES=as.data.frame(lapply(data, mean)))
    

    Simple example:

    data <- data.frame(
     x_Date = Sys.Date()+1:3,
     x_numeric = 1:3+.1,
     x_POSIXt = Sys.time()+1:3,
     x_factor = factor(letters[1:3]),
     x_character = letters[1:3],
     x_logical = c(TRUE,FALSE,TRUE),
     x_complex = 1i+1:3,
     stringsAsFactors = FALSE,
     row.names=paste("Row",1:3)
    )
    
    rbind(data, AVERAGES=as.data.frame(lapply(data , mean)))
    # Warning in mean.default(X[[4L]], ...) :
      # argument is not numeric or logical: returning NA
    # Calls: rbind -> as.data.frame -> lapply -> FUN -> mean.default
    # Warning in mean.default(X[[5L]], ...) :
      # argument is not numeric or logical: returning NA
    # Calls: rbind -> as.data.frame -> lapply -> FUN -> mean.default
                 # x_Date x_numeric            x_POSIXt x_factor x_character x_logical x_complex
    # Row 1    2010-04-21       1.1 2010-04-20 23:30:42        a           a  1.000000      1+1i
    # Row 2    2010-04-22       2.1 2010-04-20 23:30:43        b           b  0.000000      2+1i
    # Row 3    2010-04-23       3.1 2010-04-20 23:30:44        c           c  1.000000      3+1i
    # AVERAGES 2010-04-22       2.1 2010-04-20 23:30:43     <NA>        <NA>  0.666667      2+1i
    

    logical column is converted to numeric, and for non-numeric columns there are NA‘s

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

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.