0
votes

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

1 Answers

0
votes
df <- data.frame(min=sample(c(1:5),5,T),mean=sample(c(10:20),5,T),max=sample(c(25:35),5,T))

myfun <- function(data){
  data <- data %>% mutate(range=paste0('(',min,' - ',max,')'))
  return(data)
}

myfun(df)

  min mean max    range
1   3   14  35 (3 - 35)
2   3   13  29 (3 - 29)
3   2   15  27 (2 - 27)
4   2   18  35 (2 - 35)
5   3   19  33 (3 - 33)

UPDATE
This function will rename the variables as well as creating the range variable.

myfun2 <- function(data,newname='AGG'){
  data <- data %>% mutate(range=paste0('(',min,' - ',max,')'))
  names(data) <- paste0(newname,'_',names(data))
  return(data)
}

myfun2(df,'AGG')
  AGG_min AGG_mean AGG_max AGG_range
1       3       14      35  (3 - 35)
2       3       13      29  (3 - 29)
3       2       15      27  (2 - 27)
4       2       18      35  (2 - 35)
5       3       19      33  (3 - 33)