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
76 views

1 answer to this question.

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
• 46,110 points

Related Questions In Data Analytics

0 votes
2 answers

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

Try this , Employee %>% group_by(EmpID) %>% mutate(SumSalary ...READ MORE

answered Aug 14 in Data Analytics by anonymous
52 views
0 votes
2 answers

How to sum a variable by group in R?

You can also try this way, x_new = ...READ MORE

answered Jul 31 in Data Analytics by Cherukuri
• 32,260 points
11,930 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,690 points
220 views
0 votes
2 answers
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,440 points
223 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
• 57,300 points
4,380 views
0 votes
7 answers
0 votes
3 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
2,237 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
• 46,110 points
45 views