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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:12:40+00:00 2026-06-10T06:12:40+00:00

Hi I want to avoid using loops and so want to use something from

  • 0

Hi I want to avoid using loops and so want to use something from plyr to help solve my problem.

I would like to create a function that gets the sum of a specifically chosen column for each factor from a dataframe.

So if we have the following example data…

df <- data.frame(cbind(x=rnorm(100),y=rnorm(100),z=rnorm(100),f=sample(1:10,100, replace=TRUE))) 
df$f <- as.factor(df$f)

i.e. I would like something like:

foo <- function(df.obj,colname){
     some code
}

where the df.obj would be the df variable above and the colname argument could be any of x,y or z.

and I would like the output/result of the function to have a column of the unique factors (in the above case 1:10) and the sums of the values in column x for each factor.

I expect that the solution to be quite simple and would probably be using ddply or summarise somehow but can’t work out how to do it so that i can have the column name as an argument.

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-06-10T06:12:41+00:00Added an answer on June 10, 2026 at 6:12 am

    Is this what you’re after?

    > ddply(df, .(f), colwise(sum))
        f          x           y          z
    1   1 -0.4190284  2.61101681  1.2280026
    2   2  1.1063977  2.40006922  4.9550079
    3   3  0.4498366 -4.00610558  0.9964754
    4   4  1.9325488 -2.81241212 -3.1185574
    5   5 -4.1077670 -1.01232884 -3.9852388
    6   6 -1.0488003 -2.42924689  3.5273636
    7   7  2.2999306  0.85930085 -0.6245167
    8   8 -4.8105311 -6.81352238 -2.1223436
    9   9 -2.8187083  5.03391770  1.6433896
    10 10  5.1323666 -0.06192382  1.8978994
    

    Edit: correct answer as supplied by TS:

    foo <- function(df.obj,colname){ddply(df, .(f), colwise(sum))[,c("f",colname)]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how I solve the following problem using higher order
Because I want to avoid repetitive code, and I'm using a lot of text
I am using javaHg on our company's web-application. I want to avoid installing mercurial
I have a image gallery like Google+ or Facebook and i want to avoid
I want to avoid the use of for loop for iterating through the HaspMap
I'm using MVC and want to put my JSP pages in WEB-INF to avoid
I want to avoid using tagged classes and big if-else blocks or switch statement
I want avoid iterating over a nil array. My bad solution: if nil!=myArr myArr.each
I want to avoid compiling php with fileinfo, ereg, and parch. What is the
I want to avoid code duplication as much as possible. Suppose I have a

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.