How do I sum the values of a variable by group but RETAIN all records?

+1 vote

I have a data frame like this...

Ticker     Date            rec_count

 A            1/1/2019     1

A             2/1/2019     1

A             3/1/2019     1

B             1/1/2019     1

B             2/1/2019     1

B             3/1/2019     1

B             4/1/2019     1

And I basically want to create another variable SUM of the records, so that the new data table would retain all the records in the dataset and include the generated sum field.  Like this...

Ticker     Date            rec_count   sum_recs

 A            1/1/2019     1                 3

A             2/1/2019     1                 3

A             3/1/2019     1                 3

B             1/1/2019     1                 4

B             2/1/2019     1                 4

B             3/1/2019     1                 4

B             4/1/2019     1                 4

I have scoured the internet looking for a solution, but I cannot find one.  Please help!  I am new to R and while to some this may be an elementary question...it is absolutely driving me crazy!

Thanks!

Jun 4 in Data Analytics by Kirk
• 130 points
18 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Hey @Kirk, what you can do is, edit your existing dataframe by creating a new column for the variable sum_rec. Something like this:

datframe$sum_rec <- (3,3,3,4,4,4,4)
answered Jun 6 by Kalgi
• 36,300 points

Related Questions In Data Analytics

0 votes
1 answer

How can I group a set of values by column using R programming?

Use the dplyr package that R provides: library(dplyr) df1 ...READ MORE

answered Apr 25 in Data Analytics by Sophie may
• 9,330 points
25 views
0 votes
1 answer

How to sum a variable by group in R?

Easily by using Aggregate Func(): aggregate(x$points, by=list(Players=x$Players), FUN=sum) or ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,680 points

edited Apr 13, 2018 by CodingByHeart77 4,118 views
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
62 views
0 votes
1 answer
0 votes
2 answers

What is difference between Distributed search head and Search head cluster?

 A distributed environment describes the separation of ...READ MORE

answered Dec 3, 2018 in Data Analytics by Ali
• 10,380 points
113 views
+1 vote
3 answers

Problem with installation of Wordcloud in anaconda

Using Anaconda Python 3.6 version For Windows ...READ MORE

answered Aug 7, 2018 in Data Analytics by Priyaj
• 56,140 points
2,161 views
0 votes
7 answers
0 votes
2 answers

How can I add YAML current date in rmarkdown ?

<$today:MM/dd/yyyy> READ MORE

answered Mar 25 in Data Analytics by Anupam Das
1,007 views
0 votes
1 answer
0 votes
1 answer

How do I import document files in R programming

Try the following: read.table("~/SMSSpamCollection", header=TRUE, sep="\t") Change "~/" to the "ptah/to/your/file/" Mess around ...READ MORE

answered Nov 5, 2018 in Data Analytics by Kalgi
• 36,300 points
17 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.