I am trying to use a data.frame twice in a dplyr
chain. Here is a simple example that gives an error
df <- data.frame(Value=1:10,Type=rep(c("A","B"),5))
df %>%
group_by(Type) %>%
summarize(X=n()) %>%
mutate(df %>%filter(Value>2) %>%
group_by(Type) %>%
summarize(Y=sum(Value)))
Error: cannot handle
So the idea is that first a data.frame
is created with two columns Value
which is just some data and Type
which indicates which group the value is from.
I then try to use summarize to get the number of objects in each group, and then mutate, using the object again to get the sum of the values, after the data has been filtered. However I get the Error: cannot handle
. Any ideas what is happening here?
Desired Output:
Type X Y
A 5 24
B 5 28