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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:10:17+00:00 2026-06-16T08:10:17+00:00

I have a sample dataframe data as follows: X Y Month Year income 2281205

  • 0

I have a sample dataframe “data” as follows:

X            Y  Month   Year    income
2281205 228120  3   2011    1000
2281212 228121  9   2010    1100
2281213 228121  12  2010    900
2281214 228121  3   2011    9000
2281222 228122  6   2010    1111
2281223 228122  9   2010    3000
2281224 228122  12  2010    1889
2281225 228122  3   2011    778
2281243 228124  12  2010    1111
2281244 228124  3   2011    200
2281282 228128  9   2010    7889
2281283 228128  12  2010    2900
2281284 228128  3   2011    3400
2281302 228130  9   2010    1200
2281303 228130  12  2010    2000
2281304 228130  3   2011    1900
2281352 228135  9   2010    2300
2281353 228135  12  2010    1333
2281354 228135  3   2011    2340

I want to use the ddply to compute the income for each Y(not X), if I have four observations for each Y (for example for 2281223 with months 6,9,12 of 2010 and month 3 of 2011). If I have less than four observations (for example for Y =228130), I want to simply ignore it. I use the following commands in R for the above purpose:

require(plyr)
     # the data are in the data csv file
    data<-read.csv("data.csv")
    # convert Y (integers) into factors
    y<-as.factor(y)
    # get the count of each unique Y
    count<-ddply(data,.(Y), summarize, freq=length(Y))
    # get the sum of each unique Y 
    sum<-ddply(data,.(Y),summarize,tot=sum(income))
    # show the sum if number of observations for each Y is less than 4
    colbind<-cbind(count,sum)
    finalsum<-subset(colbind,freq>3)

My output are as follows:

>colbind
       Y freq      Y   tot
1 228120    1 228120  1000
2 228121    3 228121 11000
3 228122    4 228122  6778
4 228124    2 228124  1311
5 228128    3 228128 14189
6 228130    3 228130  5100
7 228135    3 228135  5973
>finalsum
       Y freq    Y.1  tot
3 228122    4 228122 6778

The above code works, but requires many steps. So,I would like to know whether there is a simple way of performing the above task (using the plyr package).

  • 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-16T08:10:19+00:00Added an answer on June 16, 2026 at 8:10 am

    As pointed out in a comment, you can do multiple operations inside the summarize.

    This reduces your code to one line of ddply() and one line of subsetting, which is easy enough with the [ operator:

    x <- ddply(data, .(Y), summarize, freq=length(Y), tot=sum(income))
    x[x$freq > 3, ]
    
           Y freq  tot
    3 228122    4 6778
    

    This is also exceptionally easy with the data.table package:

    library(data.table)
    data.table(data)[, list(freq=length(income), tot=sum(income)), by=Y][freq > 3]
            Y freq  tot
    1: 228122    4 6778
    

    In fact, the operation to calculate the length of a vector has its own shortcut in data.table – use the .N shortcut:

    data.table(data)[, list(freq=.N, tot=sum(income)), by=Y][freq > 3]
            Y freq  tot
    1: 228122    4 6778
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sample dataframe "data" as follows: X Y Month Year income 2281205
I have a dataframe as follows: > df <- data.frame(A=rnorm(26), B=rnorm(26),row.names=sample(letters,26)) I then want
I have a dataframe in R like this: dat = data.frame(Sample = c(1,1,2,2,3), Start
Let's have a dataframe with long strings in one column: df<-data.frame(short=rnorm(10,0,1),long=replicate(10,paste(rep(sample(letters),runif(1,5,8)),collapse=))) How could I
Lets have the following dataframe inside R: df <- data.frame(sample=rnorm(1,0,1),params=I(list(list(mean=0,sd=1,dist=Normal)))) df <- rbind(df,data.frame(sample=rgamma(1,5,5),params=I(list(list(shape=5,rate=5,dist=Gamma))))) df
I have a sample date DateTime startDate = DateTime.Parse(2011-01-01 00:00:00); DateTime endDate = DateTime.Parse(2011-01-03
I have a initial dataframe D . I extract two data frames from it
I have a following sample data frame: x<-c(1:4) y<-c(9:12) z<-c(a,b,c,d) data<-data.frame(x,y,z) # as data:
I have a simple data frame as follows x = data.frame(id = seq(1,10),val =
I have a dataframe that has two sets of data that I need to

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.