dplyr function group_by error

Viewed 4799

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.

2 Answers
Related