I know there's an easy answer to my question. But I tried everything. What's the problem with the following code? Why is it not grouping_by(name)? .... I tried it with and without the pipe.
df <- data.frame(name= c("jose", "jose", "maria", "maria", "maria", "pedro"),
values= c(rep(1,6)), stringsAsFactors = T)
df1 <- mutate(df, N=sum(values)) # summing without grouping
# without pipe
df <- group_by(df, name) # grouping $name
df2 <- mutate(df, N= sum(values)) # summing by group
df1 == df2 # there's no difference between results: group_by() is not working
# name values N
#[1,] TRUE TRUE TRUE
#[2,] TRUE TRUE TRUE
#[3,] TRUE TRUE TRUE
#[4,] TRUE TRUE TRUE
#[5,] TRUE TRUE TRUE
#[6,] TRUE TRUE TRUE
# with pipe
df3 <- df %>% group_by(name) %>% # grouping $name
mutate(N= sum(values)) # summing by group
df1 == df3 # there's no difference between results: group_by() is not working
# name values N
#[1,] TRUE TRUE TRUE
#[2,] TRUE TRUE TRUE
#[3,] TRUE TRUE TRUE
#[4,] TRUE TRUE TRUE
#[5,] TRUE TRUE TRUE
#[6,] TRUE TRUE TRUE
The output I want is a new column with the sum of the values by group :
df$N <- c(2,2,3,3,3,1) # DESIRED
The output I'm getting is the sum without grouping :
df$N <- c(6,6,6,6,6,6) # NOT DESIRED