Count and summarise ID's each day while creating a new column that shows the accumulated ID'sHow to simulate customer walk-ins for a given period in a fast food chain in RCreate multiple matrices from 2 bigger ones in RProbability of what product will be purchased in repeat ordersEcho-Effect-Metric Networkk-Nearest Neighbours with time series data - how to obtain whole-time-period estimatorsConvert from many sub-tables to a single tidy dataframeLook for previous date in dataframe that has certain column category in RWhat is the difference between Missing at Random and Missing not at Random data?R summarise with conditionplot the histogram of purchases
What term is being referred to with "reflected-sound-of-underground-spirits"?
Can I criticise the more senior developers around me for not writing clean code?
A strange hotel
How to fry ground beef so it is well-browned
Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?
Do I have an "anti-research" personality?
Don’t seats that recline flat defeat the purpose of having seatbelts?
How come there are so many candidates for the 2020 Democratic party presidential nomination?
Does a large simulator bay have standard public address announcements?
How to limit Drive Letters Windows assigns to new removable USB drives
Equally distributed table columns
Big O /Right or wrong?
"The cow" OR "a cow" OR "cows" in this context
Check if a string is entirely made of the same substring
How do I reattach a shelf to the wall when it ripped out of the wall?
Farming on the moon
"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?
Why did C use the -> operator instead of reusing the . operator?
How do I produce this Greek letter koppa: Ϟ in pdfLaTeX?
Retract an already submitted recommendation letter (written for an undergrad student)
Is there any official lore on the Far Realm?
Determine the application client is using to connect
What to do with someone that cheated their way through university and a PhD program?
'It addicted me, with one taste.' Can 'addict' be used transitively?
Count and summarise ID's each day while creating a new column that shows the accumulated ID's
How to simulate customer walk-ins for a given period in a fast food chain in RCreate multiple matrices from 2 bigger ones in RProbability of what product will be purchased in repeat ordersEcho-Effect-Metric Networkk-Nearest Neighbours with time series data - how to obtain whole-time-period estimatorsConvert from many sub-tables to a single tidy dataframeLook for previous date in dataframe that has certain column category in RWhat is the difference between Missing at Random and Missing not at Random data?R summarise with conditionplot the histogram of purchases
$begingroup$
I have two column, first one being the ID of a customer and second one being the Date of purchase.
ID Date
1 2017-01-17
2 2017-01-17
3 2017-01-17
4 2017-01-17
5 2017-01-17
1 2017-01-17
7 2017-01-17
1 2017-01-17
9 2017-01-18
2 2017-01-18
3 2017-01-18
5 2017-01-18
1 2017-01-18
2 2017-01-18
I would like to summarise the Purchases
made by a Customer
at one day and create a third column that shows the amount of purchases
for the customer on that date.
r
$endgroup$
add a comment |
$begingroup$
I have two column, first one being the ID of a customer and second one being the Date of purchase.
ID Date
1 2017-01-17
2 2017-01-17
3 2017-01-17
4 2017-01-17
5 2017-01-17
1 2017-01-17
7 2017-01-17
1 2017-01-17
9 2017-01-18
2 2017-01-18
3 2017-01-18
5 2017-01-18
1 2017-01-18
2 2017-01-18
I would like to summarise the Purchases
made by a Customer
at one day and create a third column that shows the amount of purchases
for the customer on that date.
r
$endgroup$
add a comment |
$begingroup$
I have two column, first one being the ID of a customer and second one being the Date of purchase.
ID Date
1 2017-01-17
2 2017-01-17
3 2017-01-17
4 2017-01-17
5 2017-01-17
1 2017-01-17
7 2017-01-17
1 2017-01-17
9 2017-01-18
2 2017-01-18
3 2017-01-18
5 2017-01-18
1 2017-01-18
2 2017-01-18
I would like to summarise the Purchases
made by a Customer
at one day and create a third column that shows the amount of purchases
for the customer on that date.
r
$endgroup$
I have two column, first one being the ID of a customer and second one being the Date of purchase.
ID Date
1 2017-01-17
2 2017-01-17
3 2017-01-17
4 2017-01-17
5 2017-01-17
1 2017-01-17
7 2017-01-17
1 2017-01-17
9 2017-01-18
2 2017-01-18
3 2017-01-18
5 2017-01-18
1 2017-01-18
2 2017-01-18
I would like to summarise the Purchases
made by a Customer
at one day and create a third column that shows the amount of purchases
for the customer on that date.
r
r
edited 15 hours ago
Stephen Rauch♦
1,52551330
1,52551330
asked Apr 6 at 11:54
JelleManneJelleManne
112
112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
This is a base solution for your problem:
aggregate(paste(ID , Date) ~ ID + Date, data = df, FUN = length)
there are many more solutions like any of the ones below, using dplyr:
library(dplyr)
df %>% group_by(ID, Date) %>% summarise(PurchaseCount = n())
df %>% group_by(ID, Date) %>% tally(name="PurchaseCount")
df %>% group_by(ID, Date) %>% count(name="PurchaseCount")
df %>% group_by(ID, Date) %>% add_tally(name="PurchaseCount")
df %>% group_by(ID, Date) %>% add_count(name="PurchaseCount")
or by using data.table package:
library(data.table)
setDT(df)[, PurchaseCount:=.N, by = list(ID, Date)]
or using sqldf package:
library(sqldf)
sqldf("SELECT ID, Date, COUNT(*) as PurchaseCount
FROM df
GROUP BY Date, ID")
or plyr:
plyr::count(df, c('ID','Date'))
I personally prefer data.table
as it is directly writes to the dataframe and often is time efficient. aggregate
is also favorable when you wanna avoid loading new libraries. dplyr
generally make your code more legible as it uses pipingpersonal opinion.
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "557"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48740%2fcount-and-summarise-ids-each-day-while-creating-a-new-column-that-shows-the-acc%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
This is a base solution for your problem:
aggregate(paste(ID , Date) ~ ID + Date, data = df, FUN = length)
there are many more solutions like any of the ones below, using dplyr:
library(dplyr)
df %>% group_by(ID, Date) %>% summarise(PurchaseCount = n())
df %>% group_by(ID, Date) %>% tally(name="PurchaseCount")
df %>% group_by(ID, Date) %>% count(name="PurchaseCount")
df %>% group_by(ID, Date) %>% add_tally(name="PurchaseCount")
df %>% group_by(ID, Date) %>% add_count(name="PurchaseCount")
or by using data.table package:
library(data.table)
setDT(df)[, PurchaseCount:=.N, by = list(ID, Date)]
or using sqldf package:
library(sqldf)
sqldf("SELECT ID, Date, COUNT(*) as PurchaseCount
FROM df
GROUP BY Date, ID")
or plyr:
plyr::count(df, c('ID','Date'))
I personally prefer data.table
as it is directly writes to the dataframe and often is time efficient. aggregate
is also favorable when you wanna avoid loading new libraries. dplyr
generally make your code more legible as it uses pipingpersonal opinion.
$endgroup$
add a comment |
$begingroup$
This is a base solution for your problem:
aggregate(paste(ID , Date) ~ ID + Date, data = df, FUN = length)
there are many more solutions like any of the ones below, using dplyr:
library(dplyr)
df %>% group_by(ID, Date) %>% summarise(PurchaseCount = n())
df %>% group_by(ID, Date) %>% tally(name="PurchaseCount")
df %>% group_by(ID, Date) %>% count(name="PurchaseCount")
df %>% group_by(ID, Date) %>% add_tally(name="PurchaseCount")
df %>% group_by(ID, Date) %>% add_count(name="PurchaseCount")
or by using data.table package:
library(data.table)
setDT(df)[, PurchaseCount:=.N, by = list(ID, Date)]
or using sqldf package:
library(sqldf)
sqldf("SELECT ID, Date, COUNT(*) as PurchaseCount
FROM df
GROUP BY Date, ID")
or plyr:
plyr::count(df, c('ID','Date'))
I personally prefer data.table
as it is directly writes to the dataframe and often is time efficient. aggregate
is also favorable when you wanna avoid loading new libraries. dplyr
generally make your code more legible as it uses pipingpersonal opinion.
$endgroup$
add a comment |
$begingroup$
This is a base solution for your problem:
aggregate(paste(ID , Date) ~ ID + Date, data = df, FUN = length)
there are many more solutions like any of the ones below, using dplyr:
library(dplyr)
df %>% group_by(ID, Date) %>% summarise(PurchaseCount = n())
df %>% group_by(ID, Date) %>% tally(name="PurchaseCount")
df %>% group_by(ID, Date) %>% count(name="PurchaseCount")
df %>% group_by(ID, Date) %>% add_tally(name="PurchaseCount")
df %>% group_by(ID, Date) %>% add_count(name="PurchaseCount")
or by using data.table package:
library(data.table)
setDT(df)[, PurchaseCount:=.N, by = list(ID, Date)]
or using sqldf package:
library(sqldf)
sqldf("SELECT ID, Date, COUNT(*) as PurchaseCount
FROM df
GROUP BY Date, ID")
or plyr:
plyr::count(df, c('ID','Date'))
I personally prefer data.table
as it is directly writes to the dataframe and often is time efficient. aggregate
is also favorable when you wanna avoid loading new libraries. dplyr
generally make your code more legible as it uses pipingpersonal opinion.
$endgroup$
This is a base solution for your problem:
aggregate(paste(ID , Date) ~ ID + Date, data = df, FUN = length)
there are many more solutions like any of the ones below, using dplyr:
library(dplyr)
df %>% group_by(ID, Date) %>% summarise(PurchaseCount = n())
df %>% group_by(ID, Date) %>% tally(name="PurchaseCount")
df %>% group_by(ID, Date) %>% count(name="PurchaseCount")
df %>% group_by(ID, Date) %>% add_tally(name="PurchaseCount")
df %>% group_by(ID, Date) %>% add_count(name="PurchaseCount")
or by using data.table package:
library(data.table)
setDT(df)[, PurchaseCount:=.N, by = list(ID, Date)]
or using sqldf package:
library(sqldf)
sqldf("SELECT ID, Date, COUNT(*) as PurchaseCount
FROM df
GROUP BY Date, ID")
or plyr:
plyr::count(df, c('ID','Date'))
I personally prefer data.table
as it is directly writes to the dataframe and often is time efficient. aggregate
is also favorable when you wanna avoid loading new libraries. dplyr
generally make your code more legible as it uses pipingpersonal opinion.
answered 19 hours ago
M-MM-M
15010
15010
add a comment |
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48740%2fcount-and-summarise-ids-each-day-while-creating-a-new-column-that-shows-the-acc%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown