How to Sort and provide top 5 values for each month in R
I have created a dataframe which has three columns
Name, Month and Amount .
The format is such that there are mutiple names in each month and each combination has an amount .
I want to find the top 5 users based on their monthly spending.
Which means the final data in the data frame will have only top 5 earnings for each month .
The way i have calculated the data now now is **
Extract_Month<- months(Credit$Transaction.Date)
Extract_Month
TopSpend<-aggregate(Credit$Amount,
by=list(Credit$User,Extract_Month)
, FUN=mean)
**
I am stuck beyond this point . Please help
Here is some sample data
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
Credit<-data.frame(User,Transaction.Date,Amount)
r
add a comment |
I have created a dataframe which has three columns
Name, Month and Amount .
The format is such that there are mutiple names in each month and each combination has an amount .
I want to find the top 5 users based on their monthly spending.
Which means the final data in the data frame will have only top 5 earnings for each month .
The way i have calculated the data now now is **
Extract_Month<- months(Credit$Transaction.Date)
Extract_Month
TopSpend<-aggregate(Credit$Amount,
by=list(Credit$User,Extract_Month)
, FUN=mean)
**
I am stuck beyond this point . Please help
Here is some sample data
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
Credit<-data.frame(User,Transaction.Date,Amount)
r
by=list(Credit$User,Credit$Transaction.Date)
?
– Chabo
Jan 18 at 20:32
This is used to Find AVg of Amounts on the basis of month and user
– Stuti Gupta
Jan 18 at 20:32
Yes, I meant why are you creating a separate variableExtract_Month
outside of the data frame and then trying to sort using it? Why not useCredit$Transaction.Date
inside the by list?
– Chabo
Jan 18 at 20:35
Yes i Corrected that later on.
– Stuti Gupta
Jan 18 at 20:40
add a comment |
I have created a dataframe which has three columns
Name, Month and Amount .
The format is such that there are mutiple names in each month and each combination has an amount .
I want to find the top 5 users based on their monthly spending.
Which means the final data in the data frame will have only top 5 earnings for each month .
The way i have calculated the data now now is **
Extract_Month<- months(Credit$Transaction.Date)
Extract_Month
TopSpend<-aggregate(Credit$Amount,
by=list(Credit$User,Extract_Month)
, FUN=mean)
**
I am stuck beyond this point . Please help
Here is some sample data
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
Credit<-data.frame(User,Transaction.Date,Amount)
r
I have created a dataframe which has three columns
Name, Month and Amount .
The format is such that there are mutiple names in each month and each combination has an amount .
I want to find the top 5 users based on their monthly spending.
Which means the final data in the data frame will have only top 5 earnings for each month .
The way i have calculated the data now now is **
Extract_Month<- months(Credit$Transaction.Date)
Extract_Month
TopSpend<-aggregate(Credit$Amount,
by=list(Credit$User,Extract_Month)
, FUN=mean)
**
I am stuck beyond this point . Please help
Here is some sample data
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
Credit<-data.frame(User,Transaction.Date,Amount)
r
r
edited Jan 19 at 6:27
Chabo
9381619
9381619
asked Jan 18 at 20:25
Stuti GuptaStuti Gupta
12
12
by=list(Credit$User,Credit$Transaction.Date)
?
– Chabo
Jan 18 at 20:32
This is used to Find AVg of Amounts on the basis of month and user
– Stuti Gupta
Jan 18 at 20:32
Yes, I meant why are you creating a separate variableExtract_Month
outside of the data frame and then trying to sort using it? Why not useCredit$Transaction.Date
inside the by list?
– Chabo
Jan 18 at 20:35
Yes i Corrected that later on.
– Stuti Gupta
Jan 18 at 20:40
add a comment |
by=list(Credit$User,Credit$Transaction.Date)
?
– Chabo
Jan 18 at 20:32
This is used to Find AVg of Amounts on the basis of month and user
– Stuti Gupta
Jan 18 at 20:32
Yes, I meant why are you creating a separate variableExtract_Month
outside of the data frame and then trying to sort using it? Why not useCredit$Transaction.Date
inside the by list?
– Chabo
Jan 18 at 20:35
Yes i Corrected that later on.
– Stuti Gupta
Jan 18 at 20:40
by=list(Credit$User,Credit$Transaction.Date)
?– Chabo
Jan 18 at 20:32
by=list(Credit$User,Credit$Transaction.Date)
?– Chabo
Jan 18 at 20:32
This is used to Find AVg of Amounts on the basis of month and user
– Stuti Gupta
Jan 18 at 20:32
This is used to Find AVg of Amounts on the basis of month and user
– Stuti Gupta
Jan 18 at 20:32
Yes, I meant why are you creating a separate variable
Extract_Month
outside of the data frame and then trying to sort using it? Why not use Credit$Transaction.Date
inside the by list?– Chabo
Jan 18 at 20:35
Yes, I meant why are you creating a separate variable
Extract_Month
outside of the data frame and then trying to sort using it? Why not use Credit$Transaction.Date
inside the by list?– Chabo
Jan 18 at 20:35
Yes i Corrected that later on.
– Stuti Gupta
Jan 18 at 20:40
Yes i Corrected that later on.
– Stuti Gupta
Jan 18 at 20:40
add a comment |
2 Answers
2
active
oldest
votes
Using made up data over multiple months. May not be the best approach but it works. I would recommend working with @NelsonGon on the tidyverse approach.
Data Creation:
library(dplyr)
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
Credit<-data.frame(User,Transaction.Date,Amount)
Aggregate, Arrange and Subset:
#Aggregate user by avg amount spent and date
TopSpend<-aggregate(Credit$Amount,
by=list(Credit$User,Credit$Transaction.Date)
, FUN=mean)
#Reverse so high in the start
TopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
print(TopSpend)
#Rename for clarity
names(TopSpend)<-c("User", "Date","Amount")
#Format date for split
TopSpend$Date<-as.POSIXct(TopSpend$Date, format="%m-%d-%Y")
#Split based on month
TopSpend_Fin<-split(TopSpend, format(TopSpend$Date, "%Y-%m"))
#Get first 5 elements (non-existent won't throw error)
TopSpend_Fin<-lapply(TopSpend_Fin, head, n = 5L)
$`2019-11`
User Date Amount
3 3 2019-11-03 300
4 2 2019-11-02 200
5 6 2019-11-01 125
$`2019-12`
User Date Amount
1 5 2019-12-02 500
2 4 2019-12-01 400
How would i get top 5 for each month
– Stuti Gupta
Jan 18 at 20:53
@StutiGupta see edits,lapply(TopSpend_Fin, head, n = 5L)
– Chabo
Jan 18 at 21:32
what about sorting in Decreasing order and then getting top 5?
– Stuti Gupta
Jan 19 at 5:56
@StutiGupta The program already sorts in decreasing order, and the top 5 is already being pulled. In the example there are not 5 total options per month so it pulls as many as it can, in decreasing order. If you want increasing order, deleteTopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
as this reverses the default which is increasing.
– Chabo
Jan 22 at 15:21
add a comment |
Here is a solution:
library(tidyverse)
df<-data.frame(Name=c("A","B","C"),Month=as.factor(c(11,11,11)),Amount=c(123,456,789))
df %>%
arrange(desc(Amount)) %>%
top_n(2,Amount)#change 2 to 5
Best to provide sample data:
iris %>%
group_by(Species) %>%
arrange(desc(Sepal.Length)) %>%
top_n(5,Sepal.Length)
OR:: Based on @Chabo 's data:
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
df1<-data.frame(Amount,Transaction.Date,User)
df1 %>%
group_by(User,Transaction.Date) %>%
arrange(desc(Amount)) %>%
top_n(5,Amount) %>%
ungroup() %>%
top_n(5,Amount)
Error in arrange_impl(.data, dots) : Evaluation error:as_dictionary()
is defunct as of rlang 0.3.0. Please useas_data_pronoun()
instead.
– Stuti Gupta
Jan 18 at 20:40
What code are you using? Reinstall tidyverse Could you also add sample data to your question to avoid us making stuff up? stackoverflow.com/questions/52957136/…
– NelsonGon
Jan 18 at 20:41
There is a different error this time .
– Stuti Gupta
Jan 18 at 20:47
Error in TopSpend %>% group_by(Group.1, Group.2) %>% arrange(desc(x)) %>% : could not find function "%>%"
– Stuti Gupta
Jan 18 at 20:47
TopSpend %>% group_by(Group.1,Group.2) %>% arrange(desc(x)) %>% top_n(5,x)
– Stuti Gupta
Jan 18 at 20:47
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f54260967%2fhow-to-sort-and-provide-top-5-values-for-each-month-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using made up data over multiple months. May not be the best approach but it works. I would recommend working with @NelsonGon on the tidyverse approach.
Data Creation:
library(dplyr)
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
Credit<-data.frame(User,Transaction.Date,Amount)
Aggregate, Arrange and Subset:
#Aggregate user by avg amount spent and date
TopSpend<-aggregate(Credit$Amount,
by=list(Credit$User,Credit$Transaction.Date)
, FUN=mean)
#Reverse so high in the start
TopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
print(TopSpend)
#Rename for clarity
names(TopSpend)<-c("User", "Date","Amount")
#Format date for split
TopSpend$Date<-as.POSIXct(TopSpend$Date, format="%m-%d-%Y")
#Split based on month
TopSpend_Fin<-split(TopSpend, format(TopSpend$Date, "%Y-%m"))
#Get first 5 elements (non-existent won't throw error)
TopSpend_Fin<-lapply(TopSpend_Fin, head, n = 5L)
$`2019-11`
User Date Amount
3 3 2019-11-03 300
4 2 2019-11-02 200
5 6 2019-11-01 125
$`2019-12`
User Date Amount
1 5 2019-12-02 500
2 4 2019-12-01 400
How would i get top 5 for each month
– Stuti Gupta
Jan 18 at 20:53
@StutiGupta see edits,lapply(TopSpend_Fin, head, n = 5L)
– Chabo
Jan 18 at 21:32
what about sorting in Decreasing order and then getting top 5?
– Stuti Gupta
Jan 19 at 5:56
@StutiGupta The program already sorts in decreasing order, and the top 5 is already being pulled. In the example there are not 5 total options per month so it pulls as many as it can, in decreasing order. If you want increasing order, deleteTopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
as this reverses the default which is increasing.
– Chabo
Jan 22 at 15:21
add a comment |
Using made up data over multiple months. May not be the best approach but it works. I would recommend working with @NelsonGon on the tidyverse approach.
Data Creation:
library(dplyr)
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
Credit<-data.frame(User,Transaction.Date,Amount)
Aggregate, Arrange and Subset:
#Aggregate user by avg amount spent and date
TopSpend<-aggregate(Credit$Amount,
by=list(Credit$User,Credit$Transaction.Date)
, FUN=mean)
#Reverse so high in the start
TopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
print(TopSpend)
#Rename for clarity
names(TopSpend)<-c("User", "Date","Amount")
#Format date for split
TopSpend$Date<-as.POSIXct(TopSpend$Date, format="%m-%d-%Y")
#Split based on month
TopSpend_Fin<-split(TopSpend, format(TopSpend$Date, "%Y-%m"))
#Get first 5 elements (non-existent won't throw error)
TopSpend_Fin<-lapply(TopSpend_Fin, head, n = 5L)
$`2019-11`
User Date Amount
3 3 2019-11-03 300
4 2 2019-11-02 200
5 6 2019-11-01 125
$`2019-12`
User Date Amount
1 5 2019-12-02 500
2 4 2019-12-01 400
How would i get top 5 for each month
– Stuti Gupta
Jan 18 at 20:53
@StutiGupta see edits,lapply(TopSpend_Fin, head, n = 5L)
– Chabo
Jan 18 at 21:32
what about sorting in Decreasing order and then getting top 5?
– Stuti Gupta
Jan 19 at 5:56
@StutiGupta The program already sorts in decreasing order, and the top 5 is already being pulled. In the example there are not 5 total options per month so it pulls as many as it can, in decreasing order. If you want increasing order, deleteTopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
as this reverses the default which is increasing.
– Chabo
Jan 22 at 15:21
add a comment |
Using made up data over multiple months. May not be the best approach but it works. I would recommend working with @NelsonGon on the tidyverse approach.
Data Creation:
library(dplyr)
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
Credit<-data.frame(User,Transaction.Date,Amount)
Aggregate, Arrange and Subset:
#Aggregate user by avg amount spent and date
TopSpend<-aggregate(Credit$Amount,
by=list(Credit$User,Credit$Transaction.Date)
, FUN=mean)
#Reverse so high in the start
TopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
print(TopSpend)
#Rename for clarity
names(TopSpend)<-c("User", "Date","Amount")
#Format date for split
TopSpend$Date<-as.POSIXct(TopSpend$Date, format="%m-%d-%Y")
#Split based on month
TopSpend_Fin<-split(TopSpend, format(TopSpend$Date, "%Y-%m"))
#Get first 5 elements (non-existent won't throw error)
TopSpend_Fin<-lapply(TopSpend_Fin, head, n = 5L)
$`2019-11`
User Date Amount
3 3 2019-11-03 300
4 2 2019-11-02 200
5 6 2019-11-01 125
$`2019-12`
User Date Amount
1 5 2019-12-02 500
2 4 2019-12-01 400
Using made up data over multiple months. May not be the best approach but it works. I would recommend working with @NelsonGon on the tidyverse approach.
Data Creation:
library(dplyr)
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
Credit<-data.frame(User,Transaction.Date,Amount)
Aggregate, Arrange and Subset:
#Aggregate user by avg amount spent and date
TopSpend<-aggregate(Credit$Amount,
by=list(Credit$User,Credit$Transaction.Date)
, FUN=mean)
#Reverse so high in the start
TopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
print(TopSpend)
#Rename for clarity
names(TopSpend)<-c("User", "Date","Amount")
#Format date for split
TopSpend$Date<-as.POSIXct(TopSpend$Date, format="%m-%d-%Y")
#Split based on month
TopSpend_Fin<-split(TopSpend, format(TopSpend$Date, "%Y-%m"))
#Get first 5 elements (non-existent won't throw error)
TopSpend_Fin<-lapply(TopSpend_Fin, head, n = 5L)
$`2019-11`
User Date Amount
3 3 2019-11-03 300
4 2 2019-11-02 200
5 6 2019-11-01 125
$`2019-12`
User Date Amount
1 5 2019-12-02 500
2 4 2019-12-01 400
edited Jan 18 at 21:57
answered Jan 18 at 20:50
ChaboChabo
9381619
9381619
How would i get top 5 for each month
– Stuti Gupta
Jan 18 at 20:53
@StutiGupta see edits,lapply(TopSpend_Fin, head, n = 5L)
– Chabo
Jan 18 at 21:32
what about sorting in Decreasing order and then getting top 5?
– Stuti Gupta
Jan 19 at 5:56
@StutiGupta The program already sorts in decreasing order, and the top 5 is already being pulled. In the example there are not 5 total options per month so it pulls as many as it can, in decreasing order. If you want increasing order, deleteTopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
as this reverses the default which is increasing.
– Chabo
Jan 22 at 15:21
add a comment |
How would i get top 5 for each month
– Stuti Gupta
Jan 18 at 20:53
@StutiGupta see edits,lapply(TopSpend_Fin, head, n = 5L)
– Chabo
Jan 18 at 21:32
what about sorting in Decreasing order and then getting top 5?
– Stuti Gupta
Jan 19 at 5:56
@StutiGupta The program already sorts in decreasing order, and the top 5 is already being pulled. In the example there are not 5 total options per month so it pulls as many as it can, in decreasing order. If you want increasing order, deleteTopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
as this reverses the default which is increasing.
– Chabo
Jan 22 at 15:21
How would i get top 5 for each month
– Stuti Gupta
Jan 18 at 20:53
How would i get top 5 for each month
– Stuti Gupta
Jan 18 at 20:53
@StutiGupta see edits,
lapply(TopSpend_Fin, head, n = 5L)
– Chabo
Jan 18 at 21:32
@StutiGupta see edits,
lapply(TopSpend_Fin, head, n = 5L)
– Chabo
Jan 18 at 21:32
what about sorting in Decreasing order and then getting top 5?
– Stuti Gupta
Jan 19 at 5:56
what about sorting in Decreasing order and then getting top 5?
– Stuti Gupta
Jan 19 at 5:56
@StutiGupta The program already sorts in decreasing order, and the top 5 is already being pulled. In the example there are not 5 total options per month so it pulls as many as it can, in decreasing order. If you want increasing order, delete
TopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
as this reverses the default which is increasing.– Chabo
Jan 22 at 15:21
@StutiGupta The program already sorts in decreasing order, and the top 5 is already being pulled. In the example there are not 5 total options per month so it pulls as many as it can, in decreasing order. If you want increasing order, delete
TopSpend<-arrange(TopSpend, rev(rownames(TopSpend)))
as this reverses the default which is increasing.– Chabo
Jan 22 at 15:21
add a comment |
Here is a solution:
library(tidyverse)
df<-data.frame(Name=c("A","B","C"),Month=as.factor(c(11,11,11)),Amount=c(123,456,789))
df %>%
arrange(desc(Amount)) %>%
top_n(2,Amount)#change 2 to 5
Best to provide sample data:
iris %>%
group_by(Species) %>%
arrange(desc(Sepal.Length)) %>%
top_n(5,Sepal.Length)
OR:: Based on @Chabo 's data:
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
df1<-data.frame(Amount,Transaction.Date,User)
df1 %>%
group_by(User,Transaction.Date) %>%
arrange(desc(Amount)) %>%
top_n(5,Amount) %>%
ungroup() %>%
top_n(5,Amount)
Error in arrange_impl(.data, dots) : Evaluation error:as_dictionary()
is defunct as of rlang 0.3.0. Please useas_data_pronoun()
instead.
– Stuti Gupta
Jan 18 at 20:40
What code are you using? Reinstall tidyverse Could you also add sample data to your question to avoid us making stuff up? stackoverflow.com/questions/52957136/…
– NelsonGon
Jan 18 at 20:41
There is a different error this time .
– Stuti Gupta
Jan 18 at 20:47
Error in TopSpend %>% group_by(Group.1, Group.2) %>% arrange(desc(x)) %>% : could not find function "%>%"
– Stuti Gupta
Jan 18 at 20:47
TopSpend %>% group_by(Group.1,Group.2) %>% arrange(desc(x)) %>% top_n(5,x)
– Stuti Gupta
Jan 18 at 20:47
|
show 5 more comments
Here is a solution:
library(tidyverse)
df<-data.frame(Name=c("A","B","C"),Month=as.factor(c(11,11,11)),Amount=c(123,456,789))
df %>%
arrange(desc(Amount)) %>%
top_n(2,Amount)#change 2 to 5
Best to provide sample data:
iris %>%
group_by(Species) %>%
arrange(desc(Sepal.Length)) %>%
top_n(5,Sepal.Length)
OR:: Based on @Chabo 's data:
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
df1<-data.frame(Amount,Transaction.Date,User)
df1 %>%
group_by(User,Transaction.Date) %>%
arrange(desc(Amount)) %>%
top_n(5,Amount) %>%
ungroup() %>%
top_n(5,Amount)
Error in arrange_impl(.data, dots) : Evaluation error:as_dictionary()
is defunct as of rlang 0.3.0. Please useas_data_pronoun()
instead.
– Stuti Gupta
Jan 18 at 20:40
What code are you using? Reinstall tidyverse Could you also add sample data to your question to avoid us making stuff up? stackoverflow.com/questions/52957136/…
– NelsonGon
Jan 18 at 20:41
There is a different error this time .
– Stuti Gupta
Jan 18 at 20:47
Error in TopSpend %>% group_by(Group.1, Group.2) %>% arrange(desc(x)) %>% : could not find function "%>%"
– Stuti Gupta
Jan 18 at 20:47
TopSpend %>% group_by(Group.1,Group.2) %>% arrange(desc(x)) %>% top_n(5,x)
– Stuti Gupta
Jan 18 at 20:47
|
show 5 more comments
Here is a solution:
library(tidyverse)
df<-data.frame(Name=c("A","B","C"),Month=as.factor(c(11,11,11)),Amount=c(123,456,789))
df %>%
arrange(desc(Amount)) %>%
top_n(2,Amount)#change 2 to 5
Best to provide sample data:
iris %>%
group_by(Species) %>%
arrange(desc(Sepal.Length)) %>%
top_n(5,Sepal.Length)
OR:: Based on @Chabo 's data:
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
df1<-data.frame(Amount,Transaction.Date,User)
df1 %>%
group_by(User,Transaction.Date) %>%
arrange(desc(Amount)) %>%
top_n(5,Amount) %>%
ungroup() %>%
top_n(5,Amount)
Here is a solution:
library(tidyverse)
df<-data.frame(Name=c("A","B","C"),Month=as.factor(c(11,11,11)),Amount=c(123,456,789))
df %>%
arrange(desc(Amount)) %>%
top_n(2,Amount)#change 2 to 5
Best to provide sample data:
iris %>%
group_by(Species) %>%
arrange(desc(Sepal.Length)) %>%
top_n(5,Sepal.Length)
OR:: Based on @Chabo 's data:
User<-c(6,2,3,4,5,6)
Transaction.Date<-c("11-1-2019","11-2-2019","11-3-2019",
"12-1-2019","12-2-2019","11-1-2019")
Amount<-c(100,200,300,400,500,150)
df1<-data.frame(Amount,Transaction.Date,User)
df1 %>%
group_by(User,Transaction.Date) %>%
arrange(desc(Amount)) %>%
top_n(5,Amount) %>%
ungroup() %>%
top_n(5,Amount)
edited Jan 19 at 1:27
Chabo
9381619
9381619
answered Jan 18 at 20:34
NelsonGonNelsonGon
2,1741623
2,1741623
Error in arrange_impl(.data, dots) : Evaluation error:as_dictionary()
is defunct as of rlang 0.3.0. Please useas_data_pronoun()
instead.
– Stuti Gupta
Jan 18 at 20:40
What code are you using? Reinstall tidyverse Could you also add sample data to your question to avoid us making stuff up? stackoverflow.com/questions/52957136/…
– NelsonGon
Jan 18 at 20:41
There is a different error this time .
– Stuti Gupta
Jan 18 at 20:47
Error in TopSpend %>% group_by(Group.1, Group.2) %>% arrange(desc(x)) %>% : could not find function "%>%"
– Stuti Gupta
Jan 18 at 20:47
TopSpend %>% group_by(Group.1,Group.2) %>% arrange(desc(x)) %>% top_n(5,x)
– Stuti Gupta
Jan 18 at 20:47
|
show 5 more comments
Error in arrange_impl(.data, dots) : Evaluation error:as_dictionary()
is defunct as of rlang 0.3.0. Please useas_data_pronoun()
instead.
– Stuti Gupta
Jan 18 at 20:40
What code are you using? Reinstall tidyverse Could you also add sample data to your question to avoid us making stuff up? stackoverflow.com/questions/52957136/…
– NelsonGon
Jan 18 at 20:41
There is a different error this time .
– Stuti Gupta
Jan 18 at 20:47
Error in TopSpend %>% group_by(Group.1, Group.2) %>% arrange(desc(x)) %>% : could not find function "%>%"
– Stuti Gupta
Jan 18 at 20:47
TopSpend %>% group_by(Group.1,Group.2) %>% arrange(desc(x)) %>% top_n(5,x)
– Stuti Gupta
Jan 18 at 20:47
Error in arrange_impl(.data, dots) : Evaluation error:
as_dictionary()
is defunct as of rlang 0.3.0. Please use as_data_pronoun()
instead.– Stuti Gupta
Jan 18 at 20:40
Error in arrange_impl(.data, dots) : Evaluation error:
as_dictionary()
is defunct as of rlang 0.3.0. Please use as_data_pronoun()
instead.– Stuti Gupta
Jan 18 at 20:40
What code are you using? Reinstall tidyverse Could you also add sample data to your question to avoid us making stuff up? stackoverflow.com/questions/52957136/…
– NelsonGon
Jan 18 at 20:41
What code are you using? Reinstall tidyverse Could you also add sample data to your question to avoid us making stuff up? stackoverflow.com/questions/52957136/…
– NelsonGon
Jan 18 at 20:41
There is a different error this time .
– Stuti Gupta
Jan 18 at 20:47
There is a different error this time .
– Stuti Gupta
Jan 18 at 20:47
Error in TopSpend %>% group_by(Group.1, Group.2) %>% arrange(desc(x)) %>% : could not find function "%>%"
– Stuti Gupta
Jan 18 at 20:47
Error in TopSpend %>% group_by(Group.1, Group.2) %>% arrange(desc(x)) %>% : could not find function "%>%"
– Stuti Gupta
Jan 18 at 20:47
TopSpend %>% group_by(Group.1,Group.2) %>% arrange(desc(x)) %>% top_n(5,x)
– Stuti Gupta
Jan 18 at 20:47
TopSpend %>% group_by(Group.1,Group.2) %>% arrange(desc(x)) %>% top_n(5,x)
– Stuti Gupta
Jan 18 at 20:47
|
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f54260967%2fhow-to-sort-and-provide-top-5-values-for-each-month-in-r%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
by=list(Credit$User,Credit$Transaction.Date)
?– Chabo
Jan 18 at 20:32
This is used to Find AVg of Amounts on the basis of month and user
– Stuti Gupta
Jan 18 at 20:32
Yes, I meant why are you creating a separate variable
Extract_Month
outside of the data frame and then trying to sort using it? Why not useCredit$Transaction.Date
inside the by list?– Chabo
Jan 18 at 20:35
Yes i Corrected that later on.
– Stuti Gupta
Jan 18 at 20:40