简体   繁体   中英

dplyr function group_by error

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.

This can be accomplished using tidy evaluation semantics. Here is an example using mtcars as no sample data was provided:

library(dplyr)

ag <- c(quo(cyl), quo(gear))

lapply(ag, function(x) mtcars %>%
         group_by(!!x) %>%
         mutate(vel1 = round(weighted.mean(hp, wt, na.rm = TRUE), 0)))

Depending on the desired output summarise might be a better suited function as it will only display one row for each group

lapply(ag, function(x) mtcars %>%
         group_by(!!x) %>%
         summarise(vel1 = round(weighted.mean(hp, wt, na.rm = TRUE), 0)))

[[1]]
# A tibble: 3 x 2
    cyl  vel1
  <dbl> <dbl>
1     4    83
2     6   122
3     8   209

[[2]]
# A tibble: 3 x 2
   gear  vel1
  <dbl> <dbl>
1     3   182
2     4    94
3     5   219

I was able to reproduce the error. Testing the code piecemeal, we find that

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))

produces this error

Error in grouped_df_impl(data, unname(vars), drop) : Column dadesCom1[, which(names(dadesCom1) == i)] can't be used as a grouping variable because it's a tbl_df/tbl/data.frame

Simply add

dadesCom1 <- as.data.frame(dadesCom1)

to the end of your first statement.

I'd also suggest using library(dplyr) and removing your in-line calls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM