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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:03:24+00:00 2026-06-14T05:03:24+00:00

I am trying to use ddply to my sample data (call Z) which look

  • 0

I am trying to use ddply to my sample data (call Z) which look like as below:

id    y
1001  10
1001  11
1200  12
2001  10
2030  12
2100  32
3100  10
3190  13
4100  45
5100  67
5670  56
...
10001  54
10345  45
11234  32
and so on

My purpose is the find the sum of the y for the id starting with 1 (i.e.1001,1200,..), 2(2100), 3(3100,3190), 4,…10,11,…65. For example, for id starting with 1 , the sum is 10+11+12=33, for id starting with 2, it is 32.

I wanted to use the apply function which looks like as follows:

>s <- split(z,z$id)
>lapply(s, function(x) colSums(x[, c("y")]))

However, this gives me the sum by each of the unique id, not the one as I was looking for. Any suggestion in this regard would be highly appreciated.

  • 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-14T05:03:25+00:00Added an answer on June 14, 2026 at 5:03 am

    thelatemail provides a valid approach but I want to point out the problem isn’t really with your understanding of lapply (your code was almost correct) but with thinking about grouping. thelatemail does this in his solution and that’s the key. I’m going to show you with your approach and then how I would actually approach this and then using ave just because I never get to use it 🙂

    Read in data

    z <- read.table(textConnection("id y #stole this from the latemail
    1001 10
    1001 11
    1200 12
    2001 10
    2030 12
    2100 32
    3100 10
    3190 13
    4100 45
    5100 67
    5670 56
    10001 54
    10345 45"),header=TRUE)
    

    Your code adjusted

    s <- split(z, substring(as.character(z$id), 1, nchar(as.character(z$id)) - 3))
    lapply(s, function(x) sum(x[, "y"]))
    

    Approach I would likely take; add a new factor id variable

    z$IDgroup <- substring(as.character(z$id), 1, nchar(as.character(z$id)) - 3)
    aggregate(y ~ IDgroup, z, sum)
    #similar approach but adds the solution back as a new column
    z$group.sum <- ave(z$y, z$IDgroup, FUN=sum)
    z
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying use epoch time dates in my series data. The array looks like
Trying to use an excpetion class which could provide location reference for XML parsing,
Hi I am trying to use ddply in the plyr library in R, with
I'm trying use to selenium with firefox on CentOS from command line like this:
I'm trying use an object which wasn't available until SDK level 5. It seems
I'm trying to use the ddply method to take a dataframe with various info
I revieving the following error when trying use mysql_real_escape() function Fatal error: Call to
i'm trying use facebook API to upload photo in my fan page. I downloaded
I am trying use gem tire to search in my application. I have tables
I was trying use a set of filter functions to run the appropriate routine,

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.