I have a problem with the functions of the dplyr package. I want a data frame to be grouped in reference to several values (group_by), some fixed and others introduced by means of a vector. This vector would have variable dimensions. Once the dataframe is grouped, I want to apply the mutate function.
I have tried to do it in several ways, the first one is what I copy below. It consists of a loop that runs through the vector campToAgregate (where are the variables depending on which we want 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 many variables, including those mentioned in the function above: "vel" and "longPk".
When compiling this code, I get the following error in the console:
Error in mutate_impl(.data, dots) : not compatible with STRSXP
And I do not know how to solve it ...
I have also tried to do it in another 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 that case the group_by is not implemented, the mutate function works, but it is applied to the ungrouped table.
Does anyone know what mistake I'm making in the codes? Thank you very much.
* Edit: the variable "dat" is a variable included in the initial database ("dades"), referring to the date of registration. This variable I want to enter in the "group_by" in all cases, apart from the variables of the vector "campToAgregate".