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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:46:19+00:00 2026-05-20T15:46:19+00:00

This is a really simple problem, but I cannot figure out how to script

  • 0

This is a really simple problem, but I cannot figure out how to script it. I cannot move forward until I figure it out. I’m really new to R and to using code, and I’m going through several introductory manuals, but haven’t found anything for this specific problem yet.

Generally, here is the issue. Let’s say I have a data frame called x that looks like:

a <- c(1995,1995,1995,1996,1997,1997,1997,1998)
b <- c(1,2,3,1,2,3,4,1)
c <- c(5,7,8,2,4,5,7,8)
(x <- data.frame(a,b,c))
     a b c
1 1995 1 5
2 1995 2 7
3 1995 3 9
4 1996 1 2
5 1997 2 4
6 1997 3 5
7 1997 4 7
8 1998 1 8

There are multiple entries for some of the years in column a (i.e. 1995 appears 3 times), when really I just want one entry for each year. If I try to plot column a against column c, I will end up with multiple points for each date, but that is not helpful. I don’t care about column b, but I want to sum entries for column c for each year, such that I end up with a data frame with one entry for each year. Given the above data, a resulting data frame would look like:

     a  c
1 1995 21
2 1996  2
3 1997 16
4 1998  8

Any ideas?

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

    The plyr library is useful for aggregation tasks such as these. plyr also plays very well with ggplot2 graphics. In my opinion, the benefit of plyr is that you explicitly define the structure of the input and output. Here we are passing in a data.frame object and also want a data.frame after processing, so we will use ddply. The first letter corresponds to the input object, and the second to the output. So if we wanted to go from a list object to data.frame, we’d use ldply, etc.

    library(ggplot2) #Loads plyr
    
    text <- "a b c
    1995 1 5
    1995 2 7
    1995 3 9
    1996 1 2
    1997 2 4
    1997 3 5
    1997 4 7
    1998 1 8
    "
    
    df <- read.table(textConnection(text), header = TRUE)
    
    #Create plotData data.frame that groups by the "a" column and returns the sum of "c"
    plotData <- ddply(df, "a", summarise, totalc = sum(c))
    
    #plotting with ggplot
    qplot(factor(a), totalc, data = plotData)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be really simple, but i cannot figure out the problem with this
hey guys having this really simple problem but cant seem to figure out have
This is a really simple problem, but I couldn't find a solution anywhere. I'm
It's a really simple problem. I've got HTML code like this : <div> <img
I am new to MVC3 and cannot move with this problem. I am making
I've got this really simple piece of code that I thought was the correct
I'm sure this is really simple, but I can't for the life of me
I'm sure this is really simple and I'm drawing a giant blank, but how
I'm sure this is really simple, but I can't seem to get it working.
I know this probably really simple but Im not sure what im doing wrong...

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.