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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:41:53+00:00 2026-05-30T15:41:53+00:00

I am using by to apply a function to a range columns of a

  • 0

I am using by to apply a function to a range columns of a data frame based on a factor. Everything works perfectly well if I use mean() as the function but if I use median() I get an error of the type “Error in median.default(x) : need numeric data” even if I don’t have NAs in the data frame.

The line that works using mean():

by(iris[,1:3], iris$Species, function(x) mean(x,na.rm=T))

> by(iris[,1:3], iris$Species, function(x) mean(x,na.rm=T))
iris$Species: setosa
Sepal.Length  Sepal.Width Petal.Length 
       5.006        3.428        1.462 
------------------------------------------------------------ 
iris$Species: versicolor
Sepal.Length  Sepal.Width Petal.Length 
       5.936        2.770        4.260 
------------------------------------------------------------ 
iris$Species: virginica
Sepal.Length  Sepal.Width Petal.Length 
       6.588        2.974        5.552 
Warning messages:
1: mean(<data.frame>) is deprecated.
 Use colMeans() or sapply(*, mean) instead. 
2: mean(<data.frame>) is deprecated.
 Use colMeans() or sapply(*, mean) instead. 
3: mean(<data.frame>) is deprecated.
 Use colMeans() or sapply(*, mean) instead. 

But if I use median() (note the na.rm=T option):

> by(iris[,1:3], iris$Species, function(x) median(x,na.rm=T))
Error in median.default(x, na.rm = T) : need numeric data

However if instead of choosing the range [,1:3] of columns I choose only one of the columns it works:

> by(iris[,1], iris$Species, function(x) median(x,na.rm=T))
iris$Species: setosa
[1] 5
------------------------------------------------------------ 
iris$Species: versicolor
[1] 5.9
------------------------------------------------------------ 
iris$Species: virginica
[1] 6.5

How can I achieve this behaviour while selecting a range of columns?

  • 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-30T15:41:54+00:00Added an answer on May 30, 2026 at 3:41 pm

    You are using a split-apply strategy when you use by. The objects being passed to the function are dataframes and you are getting the warning and error because of the non-existence of median.data.frame and the impending non-existence of mean.data.frame. It might work better if you used aggregate:

    > aggregate(iris[,1:3], iris["Species"], function(x) mean(x,na.rm=T))
         Species Sepal.Length Sepal.Width Petal.Length
    1     setosa        5.006       3.428        1.462
    2 versicolor        5.936       2.770        4.260
    3  virginica        6.588       2.974        5.552
    > aggregate(iris[,1:3], iris["Species"], function(x) median(x,na.rm=T))
         Species Sepal.Length Sepal.Width Petal.Length
    1     setosa          5.0         3.4         1.50
    2 versicolor          5.9         2.8         4.35
    3  virginica          6.5         3.0         5.55
    

    aggregate works on the column vectors individually and then tabulates the results.

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

Sidebar

Related Questions

I want to convert variables into factors using apply() : a <- data.frame(x1 =
How can we apply attributes to class function using AOP in C#? UPDATE: I
I have a SqlDump.sql file that works just fine when I apply it using
I'm trying to invoke addEventListener() using apply() method. The code is like: function rewrite(old){
In R, I want to call apply on a huge data.frame, and write values
What is the difference between using Function.prototype.apply() and Function.prototype.call() to invoke a function? const
How to apply a function to every element in a list using Linq in
I have to apply regression function progressively to a time series data (vector time
Using Mathematica, I have a list: l={0,0,0,1,2,0,0,0,1,0,0,0,2,0,0,0} I want to apply a function to
I'm currently using Function.apply to call a function with a dynamic number of arguments,

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.