I am trying to create a function to optimize my code. I have tibbles in the following format. I want to add a 5th and final column that is a titled "AGG_range."
>data.temp0
AGG_median AGG_mean AGG_min AGG_max
<dbl> <dbl> <dbl> <dbl>
1 38 40.7 29 52
2 49 49 21 83
3 30 39 10 75
4 38 38.9 13 59
5 63.5 57.4 15 82
6 32 33 21 50
7 39 40.5 14 75
I have tried using mutate(!!varname = paste(!!varname2,!!varname3,sep="-") AND mutate_(somename = paste(varname2,varna,sep="-"))
However the results I am getting is not what I want.
A simple example of what I am doing is the following:
myfuntemp = function(chr_temp){
chr_temp.range = chr_temp %>%
paste("range",sep="_")
chr_temp.min = chr_temp %>%
paste("min",sep="_")
chr_temp.max = chr_temp %>%
paste("max",sep="_")
dat_temp = data.temp0 %>%
mutate_(chr_temp.range = paste("(",as.character(chr_temp.min),"-",
as.character(chr_temp.max),")",sep="")) %>%
rename (!!chr_temp.range:= chr_temp.range)
return(dat_temp)
}
The following is what I get (actual output)
>myfuntemp('AGG')
AGG_median AGG_mean AGG_min AGG_max AGG_range
<dbl> <dbl> <dbl> <dbl> <dbl>
1 38 40.7 29 52 -23
2 49 49 21 83 -62
3 30 39 10 75 -65
4 38 38.9 13 59 -46
5 63.5 57.4 15 82 -67
6 32 33 21 50 -29
7 39 40.5 14 75 -61
However, the following is what I want (expected output)
AGG_median AGG_mean AGG_min AGG_max AGG_range
<dbl> <dbl> <dbl> <dbl> <chr>
1 38 40.7 29 52 (29-52)
2 49 49 21 83 (21-83)
3 30 39 10 75 (10-75)
4 38 38.9 13 59 (13-59)
5 63.5 57.4 15 82 (15-82)
6 32 33 21 50 (21-50)
7 39 40.5 14 75 (14-75)