I have problems with a function of library dplyr. I want to group a dataframe by various values ("group_by"). Some of these values are fix (always the same), and some are introduced through a vector. This vector would have variable dimensions. When the data frame will be grouped, I want to apply the function "mutate".
I have tryed to do it by different ways. The first is copied below, and includes a loop that goes over the vector campToAgregate (where are located the values needed to group the dataframe):
campToAgregate = c("via","nomDem")
dadesCom <- dades
for(i in 1:length(campToAgregate)){
if(i==1){
dadesCom1 <- dadesCom %>% dplyr::group_by(dadesCom[,which(names(dadesCom) == campToAgregate[i])], dat, add=TRUE) %>%
dplyr::mutate(vel1 = round(weighted.mean(vel, longPk, na.rm = TRUE), 0))
dadesCom1 <- dadesCom1[,-(ncol(dadesCom1)-1)]
}else{
dadesCom2 <- dadesCom1 %>% dplyr::group_by(dadesCom1[,which(names(dadesCom1) == campToAgregate[i])], add=TRUE) %>%
dplyr::mutate(vel1 = round(weighted.mean(vel, longPk, na.rm = TRUE), 0))
}
}
dades is the dataframe, and it contains a lot of values, including the values that are mentioned in the function above: "vel" and "longPk".
When I run this code, the following error appears in the console:
Error in mutate_impl(.data, dots) : not compatible with STRSXP
And I don't know how to solve it...
I have also tryed to do it by a different way:
for(i in 1:length(campToAgregate)){
if(i==1){
dadesCom <- dadesCom %>% dplyr::group_by(dadesCom[,which(names(dadesCom) == campToAgregate[i])], dat, add=TRUE)
}else{
dadesCom <- dadesCom %>% dplyr::group_by(dadesCom1[,which(names(dadesCom1) == campToAgregate[i])], add=TRUE)
}
}
dadesCom <- dadesCom %>% dplyr::mutate(vel = round(weighted.mean(vel, longPk, na.rm = TRUE), 0))
But in this case the function group_by doesn't work. The mutate function works, but it's applied to the dataframe without group.
Does anybody know what kind of mistakes I'm doing in the code? Thank you.
dat
defined anywhere, and you seem to by trying to group by a data frame.group_by
expects unquoted variable names. In your case, you don't always know the variables names, and so unquoting them can be a challenge. This is a case wheregroup_by_at
is useful, as you can pass a character vector of variable names to the.vars
argument and get the same effect. – Benjamin