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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:51:04+00:00 2026-05-20T23:51:04+00:00

I have a data.frame with 20 columns. The first two are factors, and the

  • 0

I have a data.frame with 20 columns. The first two are factors, and the rest are numeric. I’d like to use the first two columns as split variables and then apply the mean() to the remaining columns.

This seems like a quick and easy job for ddply(), however, the results for the output data.frame are not what I am looking for. Here is a minimal example with just one column of data:

Aa <- c(rep(c("A", "a"), each = 20))
Bb <- c(rep(c("B", "b", "B", "b"), each = 10))
x <- runif(40)
df1 <- data.frame(Aa, Bb, x)

ddply(df1, .(Aa, Bb), mean)

The output is:

  Aa Bb         x
1 NA NA 0.5193275
2 NA NA 0.4491907
3 NA NA 0.4848128
4 NA NA 0.4717899
Warning messages:
1: In mean.default(X[[1L]], ...) :
  argument is not numeric or logical: returning NA

The warning is repeated 8 times, presumably once for each call to mean(). I’m guessing this comes from trying to take the mean of a factor. I could write this as:

ddply(df1, .(Aa, Bb), function(df1) mean(df1$x))

or

ddply(df1, .(Aa, Bb), summarize, x = mean(x))

both of which do work (not giving NAs), but I would rather avoid writing out 18 such x = mean(x) statements, one for each of my numeric columns.

Is there a general solution? I’m not wedded to ddply if there is a better answer elsewhere.

  • 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-20T23:51:04+00:00Added an answer on May 20, 2026 at 11:51 pm

    Since you are reducing hte number of rows, you need to use summarise:

    > ddply(df1, .(Aa, Bb), summarise, mean_x =mean(x) )
      Aa Bb    mean_x
    1  a  b 0.3790675
    2  a  B 0.4242922
    3  A  b 0.5622329
    4  A  B 0.4574471
    

    It’s just as easy to use aggregate in this instance. Let’s say you had two variables:

    > aggregate(df1[-(1:2)], df1[1:2], mean)
      Aa Bb         x         y
    1  a  b 0.4249121 0.4639192
    2  A  b 0.6127175 0.4639192
    3  a  B 0.4522292 0.4826715
    4  A  B 0.5201965 0.4826715
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with two columns. First column contains categories such as
I have a data frame with 22239 rows & 200 columns. The first column
I have a data.frame with 2 columns: Node A, Node B. Each entry in
I have a data frame where some of the columns contain NA values. How
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame with gaps like this: Var1 Var2 Var3 1 NA
I have two data.frames. For examples sake let's say they look like this: df1
I have two csv files: csv1 <- data.frame(y=c(classA, classB, classA, classB, classA, classC), DBID=c(d1,
Assuming in R, I have a data.frame with the first column representing the time
I have a data frame containing multiple time series of returns, stored in columns.

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.