简体   繁体   中英

Dynamic mean of consecutive columns in dplyr

I have a data frame with a large number of columns containing numeric values. I'd like to dynamically calculate the mean value of the two consecutive columns (so mean of column 1 and column 2, mean of column 3 and 4, mean of 5 and 6 etc...) and either store it into new column names or replace one of the two columns I used in the calculation.

I tried creating a function that calculate the mean of two columns and storing it into the first column then applying a loop to that function so it applies to my whole datatable.

However I'm struggling with mutate : since I dynamically generate the name of the column I use (they all start with "PUISSANCE" then a number) through a glue, it displays the value as a string into the mutate and doesn't evaluate it.

mean_col <- function(data, k) 
{
    n<-2*k+1  
    m<-2*k+2
    varname_even <- paste("PUISSANCE", m,sep="")
    varname_odd <- paste("PUISSANCE", n,sep="")
    mutate(data, "{{varname_odd}}" := ({{varname_odd}}+{{varname_even}})/2)   %% *here is the issue, the argument on the right is considered as non numeric, since it is the sum of two strings...*
    data
}
for (k in 0:24) {
  my_data_set <- mean_col(my_data_set,k)
}

Ok guys, just to let you know that I managed to solve it myself. I did a pivot_longer transmute in order to put all the "PuissanceXX" in the same column and the values associated in another. Then I used str_extract to get only the number XX from the string "PUISSANCEXX" that I converted into a numeric. Thanks to a division by 2 (-0,5) I managed to have each successive value being X and X,5. so getting both values to X thanks to a floor. Then I just did a group_by/summarize in order to get the sum and that's it !

pivot_longer(starts_with("PUISSANCE"),names_to = "heure", values_to = "puissance") %>%
mutate("time" = floor(as.numeric(str_extract(heure, "\\d+"))/2-0.5)) %>%
select(-heure) %>% 
group_by(time) %>%
summarise("power" = mean(puissance))

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