Search code examples
rfunctiontidyversereshape2

Writing function...receiving error that variable isn't found


I can't figure out why I'm getting the error

Error: object 'grade' not found

when composing a function.

Grade is obviously in the dataset and is included in the function. If I don't use the function and just use

dat%>%
cohort.fun()%>%
group_by(cohort, variable, timepoint)%>%
    summarize(perf_measure = mean(measure))

everything works fine. These are the two functions I'm using:

library(reshape2)
library(tidyverse)

cohort.fun <- function(dat){
  dat%>%
    mutate(grade = as.numeric(grade))%>%
    mutate(cohort = ifelse(grade%in%c(3,4),3,ifelse(
      grade%in%c(5,6), 5, ifelse(
        grade%in%c(7,8), 7, grade))))%>%
    mutate(cohort = as.character(cohort))
}

melt.fun <- function(dat){
   melt(c("pid", "grade", "timepoint"), value.name = "measure")%>%
  cohort.fun()
}

then I run

dat%>%
melt.fun()

and I get the error above. Any ideas? Thanks much!

Here's the dput:

structure(list(pid = c("ADMIN-UCSF-bo004", "ADMIN-UCSF-bo005", 
"ADMIN-UCSF-bo008", "ADMIN-UCSF-bo010", "ADMIN-UCSF-bo011", "ADMIN-UCSF-bo012", 
"ADMIN-UCSF-bo013", "ADMIN-UCSF-bo014", "ADMIN-UCSF-bo015", "ADMIN-UCSF-bo016"
), grade = c("3", "3", "3", "3", "3", "3", "3", "3", "3", "3"
), RC1 = c(-1.81295211570392, -1.31252376878321, -1.1701654183369, 
-1.58244557144815, -1.95383829351231, -0.516109923323212, -0.370765686983851, 
-1.93212644807752, -1.6241046548069, -1.34160382084709), RC2 = c(-0.363819589341912, 
0.268206917949323, -2.24123725035034, -0.25274997192688, 0.313608190056975, 
-0.0393486670413662, -0.0623610937831014, 0.803692668734253, 
0.416065992573585, -0.069880541013785), RC3 = c(-2.69157047028032, 
-0.822917456389246, -1.52186068360016, -0.590070546800741, 0.583790188582597, 
-0.253888391947117, 1.22197349838073, -1.63335701437031, 1.24595192142446, 
0.0191275904777839), timepoint = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-10L))

Solution

  • In your function melt.fun what you are actually passing to cohort.fun is the result of melt and not dat. Ergo cohort.fun does not find its parameter.

    Defining melt.fun as follows:

    melt.fun <- function(dat){
       melt(dat, c("pid", "grade", "timepoint"), value.name = "measure") %>%
      cohort.fun()
    }
    

    Should do the trick. You can see the use of the magrittr's forward pipe operators here

    EDIT: I'm including the whole script here so you guys can see what I've done so far:

    #Loading libraries
    library(tidyverse)
    library(reshape2)
    
    #Loading data
    dat <- structure(list(pid = c("ADMIN-UCSF-bo004", "ADMIN-UCSF-bo005", 
                                  "ADMIN-UCSF-bo008", "ADMIN-UCSF-bo010", "ADMIN-UCSF-bo011", "ADMIN-UCSF-bo012", "ADMIN-UCSF-bo013", "ADMIN-UCSF-bo014", "ADMIN-UCSF-bo015", "ADMIN-UCSF-bo016"), grade = c("3", "3", "3", "3", "3", "3", "3", "3", "3", "3"), RC1 = c(-1.81295211570392, -1.31252376878321, -1.1701654183369, -1.58244557144815, -1.95383829351231, -0.516109923323212, -0.370765686983851, -1.93212644807752, -1.6241046548069, -1.34160382084709), RC2 = c(-0.363819589341912, 0.268206917949323, -2.24123725035034, -0.25274997192688, 0.313608190056975, -0.0393486670413662, -0.0623610937831014, 0.803692668734253, 0.416065992573585, -0.069880541013785), RC3 = c(-2.69157047028032, -0.822917456389246, -1.52186068360016, -0.590070546800741, 0.583790188582597, -0.253888391947117, 1.22197349838073, -1.63335701437031, 1.24595192142446, 0.0191275904777839), timepoint = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L))
    
    #Defining functions
    #Function1
    cohort.fun <- function(dat){
      dat%>%
        mutate(grade = as.numeric(grade))%>%
        mutate(cohort = ifelse(grade%in%c(3,4),3,ifelse(
          grade%in%c(5,6), 5, ifelse(
            grade%in%c(7,8), 7, grade))))%>%
        mutate(cohort = as.character(cohort))
    }
    
    #Function2
    melt.fun <- function(dat){
      melt(dat, c("pid", "grade", "timepoint"), value.name = "measure") %>%
      cohort.fun()
    }
    
    #Executing
    dat%>%
      melt.fun()
    
    #Result
    > dat%>%
    +   melt.fun()
                    pid grade timepoint variable     measure cohort
    1  ADMIN-UCSF-bo004     3         1      RC1 -1.81295212      3
    2  ADMIN-UCSF-bo005     3         1      RC1 -1.31252377      3
    3  ADMIN-UCSF-bo008     3         1      RC1 -1.17016542      3
    4  ADMIN-UCSF-bo010     3         1      RC1 -1.58244557      3
    5  ADMIN-UCSF-bo011     3         1      RC1 -1.95383829      3
    6  ADMIN-UCSF-bo012     3         1      RC1 -0.51610992      3
    7  ADMIN-UCSF-bo013     3         1      RC1 -0.37076569      3
    8  ADMIN-UCSF-bo014     3         1      RC1 -1.93212645      3
    9  ADMIN-UCSF-bo015     3         1      RC1 -1.62410465      3
    10 ADMIN-UCSF-bo016     3         1      RC1 -1.34160382      3
    11 ADMIN-UCSF-bo004     3         1      RC2 -0.36381959      3
    12 ADMIN-UCSF-bo005     3         1      RC2  0.26820692      3
    13 ADMIN-UCSF-bo008     3         1      RC2 -2.24123725      3
    14 ADMIN-UCSF-bo010     3         1      RC2 -0.25274997      3
    15 ADMIN-UCSF-bo011     3         1      RC2  0.31360819      3
    16 ADMIN-UCSF-bo012     3         1      RC2 -0.03934867      3
    17 ADMIN-UCSF-bo013     3         1      RC2 -0.06236109      3
    18 ADMIN-UCSF-bo014     3         1      RC2  0.80369267      3
    19 ADMIN-UCSF-bo015     3         1      RC2  0.41606599      3
    20 ADMIN-UCSF-bo016     3         1      RC2 -0.06988054      3
    21 ADMIN-UCSF-bo004     3         1      RC3 -2.69157047      3
    22 ADMIN-UCSF-bo005     3         1      RC3 -0.82291746      3
    23 ADMIN-UCSF-bo008     3         1      RC3 -1.52186068      3
    24 ADMIN-UCSF-bo010     3         1      RC3 -0.59007055      3
    25 ADMIN-UCSF-bo011     3         1      RC3  0.58379019      3
    26 ADMIN-UCSF-bo012     3         1      RC3 -0.25388839      3
    27 ADMIN-UCSF-bo013     3         1      RC3  1.22197350      3
    28 ADMIN-UCSF-bo014     3         1      RC3 -1.63335701      3
    29 ADMIN-UCSF-bo015     3         1      RC3  1.24595192      3
    30 ADMIN-UCSF-bo016     3         1      RC3  0.01912759      3