Re: [R] help with recursive function
recursive_funlp <- function(dataset = dat1, func = funlp2) { ... if (!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() # GIVE SOME ARGUMENTS HERE return(df2) } } When you recurse into recursve_funip(), give it at least a new dataset argument. Otherwise it just processes the entire dat1 again and the recursion never stops. Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Dec 14, 2017 at 9:11 AM, DIGHE, NILESH [AG/2362] < nilesh.di...@monsanto.com> wrote: > Eric: I will try and see if I can figure out the issue by debugging as > you suggested. I don’t know why my code after stopifnot is not getting > executed where I like the code to run the funlp2 function when the if > statement is TRUE but when it is false, I like it to keep running until the > stopifnot condition is met. > > When the stopifnot condition is met, I like to get the output from if > statement saved. > Anyway, I will keep trying. > Again, Thanks for your help! > Nilesh > > From: Eric Berger [mailto:ericjber...@gmail.com] > Sent: Thursday, December 14, 2017 10:29 AM > To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> > Cc: r-help <r-help@r-project.org> > Subject: Re: [R] help with recursive function > > If you are trying to understand why the "stopifnot" condition is met you > can replace it by something like: > > if ( any(dat2$norm_sd >= 1) ) >browser() > > This will put you in a debugging session where you can examine your > variables, e.g. > > > dat$norm_sd > > HTH, > Eric > > > > On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjber...@gmail.com > <mailto:ericjber...@gmail.com>> wrote: > The message is coming from your stopifnot() condition being met. > > > On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] < > nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: > Hi, I accidently left out few lines of code from the calclp function. > Updated function is pasted below. > I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is > not TRUE“ > > I would appreciate any help. > Nilesh > dput(calclp) > function (dataset) > { > dat1 <- funlp1(dataset) > recursive_funlp <- function(dataset = dat1, func = funlp2) { > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% > spread(key = field_rep, value = lp) %>% mutate_at(.vars = > grep("_", > names(.)), funs(norm = round(scale(.), 3))) > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > 1, sd, na.rm = TRUE), 3) > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > 1, function(x) { > max(abs(x), na.rm = TRUE) > }), 3) > data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, > -norm_sd) %>% separate(key, c("field_rep", "treatment"), > "\\.") %>% spread(treatment, value) %>% > mutate(outlier = NA) > stopifnot(!(any(data1$norm_sd >= 1))) > if (!(any(data1$norm_sd >= 1))) { > df1 <- dat1 > return(df1) > } > else { > df2 <- recursive_funlp() > return(df2) > } > } > df3 <- recursive_funlp(dataset = dat1, func = funlp2) > df3 > } > > > From: DIGHE, NILESH [AG/2362] > Sent: Thursday, December 14, 2017 9:01 AM > To: 'Eric Berger' <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> > Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> > Subject: RE: [R] help with recursive function > > Eric: Thanks for taking time to look into my problem. Despite of making > the change you suggested, I am still getting the same error. I am > wondering if the logic I am using in the stopifnot and if functions is a > problem. > I like the recursive function to stop whenever the norm_sd column has zero > values that are above or equal to 1. Below is the calclp function after the > changes you suggested. > Thanks. Nilesh > > dput(calclp) > function (dataset) > { > dat1 <- funlp1(dataset) > recursive_funlp <- function(dataset = dat1, func = funlp2) { > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% >
Re: [R] help with recursive function
When I run the code without stopifnot, the code takes 5 min to run and then it throws an error listed below without producing any results. Error: node stack overflow In addition: There were 50 or more warnings (use warnings() to see the first 50) Error during wrapup: node stack overflow Thanks. Nilesh From: William Dunlap [mailto:wdun...@tibco.com] Sent: Thursday, December 14, 2017 11:26 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> Cc: Eric Berger <ericjber...@gmail.com>; r-help <r-help@r-project.org> Subject: Re: [R] help with recursive function Your code contains the lines stopifnot(!(any(data1$norm_sd >= 1))) if(!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } stop() "throws an error", causing the current function and all functions in the call stack to abort and return nothing. It does not mean to stop now and return a result. Does the function give the correct results if you just leave out the stopifnot line? Bill Dunlap TIBCO Software wdunlap tibco.com<http://tibco.com> On Thu, Dec 14, 2017 at 9:11 AM, DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: Eric: I will try and see if I can figure out the issue by debugging as you suggested. I don’t know why my code after stopifnot is not getting executed where I like the code to run the funlp2 function when the if statement is TRUE but when it is false, I like it to keep running until the stopifnot condition is met. When the stopifnot condition is met, I like to get the output from if statement saved. Anyway, I will keep trying. Again, Thanks for your help! Nilesh From: Eric Berger [mailto:ericjber...@gmail.com<mailto:ericjber...@gmail.com>] Sent: Thursday, December 14, 2017 10:29 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> Subject: Re: [R] help with recursive function If you are trying to understand why the "stopifnot" condition is met you can replace it by something like: if ( any(dat2$norm_sd >= 1) ) browser() This will put you in a debugging session where you can examine your variables, e.g. > dat$norm_sd HTH, Eric On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjber...@gmail.com<mailto:ericjber...@gmail.com><mailto:ericjber...@gmail.com<mailto:ericjber...@gmail.com>>> wrote: The message is coming from your stopifnot() condition being met. On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com><mailto:nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>>> wrote: Hi, I accidently left out few lines of code from the calclp function. Updated function is pasted below. I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is not TRUE“ I would appreciate any help. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, -norm_sd) %>% separate(key, c("field_rep", "treatment"), "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) stopifnot(!(any(data1$norm_sd >= 1))) if (!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: DIGHE, NILESH [AG/2362] Sent: Thursday, December 14, 2017 9:01 AM To: 'Eric Berger' <ericjber...@gmail.com<mailto:ericjber...@gmail.com><mailto:ericjber...@gmail.com<mailto:ericjber...@gmail.com>>> Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org><mailto:r-help@r-project.org<mailto:r-help@r-project.org>>> Subject: RE: [R] help with recursive function Eric: Thanks for taking time to look into my problem. Despite of making the change you suggested, I am still getting the same error. I am wondering if the l
Re: [R] help with recursive function
Your code contains the lines stopifnot(!(any(data1$norm_sd >= 1))) if(!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } stop() "throws an error", causing the current function and all functions in the call stack to abort and return nothing. It does not mean to stop now and return a result. Does the function give the correct results if you just leave out the stopifnot line? Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Dec 14, 2017 at 9:11 AM, DIGHE, NILESH [AG/2362] < nilesh.di...@monsanto.com> wrote: > Eric: I will try and see if I can figure out the issue by debugging as > you suggested. I don’t know why my code after stopifnot is not getting > executed where I like the code to run the funlp2 function when the if > statement is TRUE but when it is false, I like it to keep running until the > stopifnot condition is met. > > When the stopifnot condition is met, I like to get the output from if > statement saved. > Anyway, I will keep trying. > Again, Thanks for your help! > Nilesh > > From: Eric Berger [mailto:ericjber...@gmail.com] > Sent: Thursday, December 14, 2017 10:29 AM > To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> > Cc: r-help <r-help@r-project.org> > Subject: Re: [R] help with recursive function > > If you are trying to understand why the "stopifnot" condition is met you > can replace it by something like: > > if ( any(dat2$norm_sd >= 1) ) >browser() > > This will put you in a debugging session where you can examine your > variables, e.g. > > > dat$norm_sd > > HTH, > Eric > > > > On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjber...@gmail.com > <mailto:ericjber...@gmail.com>> wrote: > The message is coming from your stopifnot() condition being met. > > > On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] < > nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: > Hi, I accidently left out few lines of code from the calclp function. > Updated function is pasted below. > I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is > not TRUE“ > > I would appreciate any help. > Nilesh > dput(calclp) > function (dataset) > { > dat1 <- funlp1(dataset) > recursive_funlp <- function(dataset = dat1, func = funlp2) { > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% > spread(key = field_rep, value = lp) %>% mutate_at(.vars = > grep("_", > names(.)), funs(norm = round(scale(.), 3))) > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > 1, sd, na.rm = TRUE), 3) > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > 1, function(x) { > max(abs(x), na.rm = TRUE) > }), 3) > data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, > -norm_sd) %>% separate(key, c("field_rep", "treatment"), > "\\.") %>% spread(treatment, value) %>% > mutate(outlier = NA) > stopifnot(!(any(data1$norm_sd >= 1))) > if (!(any(data1$norm_sd >= 1))) { > df1 <- dat1 > return(df1) > } > else { > df2 <- recursive_funlp() > return(df2) > } > } > df3 <- recursive_funlp(dataset = dat1, func = funlp2) > df3 > } > > > From: DIGHE, NILESH [AG/2362] > Sent: Thursday, December 14, 2017 9:01 AM > To: 'Eric Berger' <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> > Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> > Subject: RE: [R] help with recursive function > > Eric: Thanks for taking time to look into my problem. Despite of making > the change you suggested, I am still getting the same error. I am > wondering if the logic I am using in the stopifnot and if functions is a > problem. > I like the recursive function to stop whenever the norm_sd column has zero > values that are above or equal to 1. Below is the calclp function after the > changes you suggested. > Thanks. Nilesh > > dput(calclp) > function (dataset) > { > dat1 <- funlp1(dataset) > recursive_funlp <- function(dataset = dat1, func = funlp2) { > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% >
Re: [R] help with recursive function
Eric: I will try and see if I can figure out the issue by debugging as you suggested. I don’t know why my code after stopifnot is not getting executed where I like the code to run the funlp2 function when the if statement is TRUE but when it is false, I like it to keep running until the stopifnot condition is met. When the stopifnot condition is met, I like to get the output from if statement saved. Anyway, I will keep trying. Again, Thanks for your help! Nilesh From: Eric Berger [mailto:ericjber...@gmail.com] Sent: Thursday, December 14, 2017 10:29 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> Cc: r-help <r-help@r-project.org> Subject: Re: [R] help with recursive function If you are trying to understand why the "stopifnot" condition is met you can replace it by something like: if ( any(dat2$norm_sd >= 1) ) browser() This will put you in a debugging session where you can examine your variables, e.g. > dat$norm_sd HTH, Eric On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> wrote: The message is coming from your stopifnot() condition being met. On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: Hi, I accidently left out few lines of code from the calclp function. Updated function is pasted below. I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is not TRUE“ I would appreciate any help. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, -norm_sd) %>% separate(key, c("field_rep", "treatment"), "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) stopifnot(!(any(data1$norm_sd >= 1))) if (!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: DIGHE, NILESH [AG/2362] Sent: Thursday, December 14, 2017 9:01 AM To: 'Eric Berger' <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> Subject: RE: [R] help with recursive function Eric: Thanks for taking time to look into my problem. Despite of making the change you suggested, I am still getting the same error. I am wondering if the logic I am using in the stopifnot and if functions is a problem. I like the recursive function to stop whenever the norm_sd column has zero values that are above or equal to 1. Below is the calclp function after the changes you suggested. Thanks. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) stopifnot(!(any(dat2$norm_sd >= 1))) if (!(any(dat2$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: Eric Berger [mailto:ericjber...@gmail.com] Sent: Thursday, December 14, 2017 8:17 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> Subject: Re: [R] help with recursive function My own typo ... whoops ...
Re: [R] help with recursive function
If you are trying to understand why the "stopifnot" condition is met you can replace it by something like: if ( any(dat2$norm_sd >= 1) ) browser() This will put you in a debugging session where you can examine your variables, e.g. > dat$norm_sd HTH, Eric On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjber...@gmail.com> wrote: > The message is coming from your stopifnot() condition being met. > > > On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] < > nilesh.di...@monsanto.com> wrote: > >> Hi, I accidently left out few lines of code from the calclp function. >> Updated function is pasted below. >> >> I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is >> not TRUE“ >> >> >> >> I would appreciate any help. >> >> Nilesh >> >> dput(calclp) >> >> function (dataset) >> >> { >> >> dat1 <- funlp1(dataset) >> >> recursive_funlp <- function(dataset = dat1, func = funlp2) { >> >> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% >> >> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% >> >> spread(key = field_rep, value = lp) %>% mutate_at(.vars = >> grep("_", >> >> names(.)), funs(norm = round(scale(.), 3))) >> >> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, sd, na.rm = TRUE), 3) >> >> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", >> names(dat2))], >> >> 1, function(x) { >> >> max(abs(x), na.rm = TRUE) >> >> }), 3) >> >> data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, >> >> -norm_sd) %>% separate(key, c("field_rep", "treatment"), >> >> "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) >> >> stopifnot(!(any(data1$norm_sd >= 1))) >> >> if (!(any(data1$norm_sd >= 1))) { >> >> df1 <- dat1 >> >> return(df1) >> >> } >> >>else { >> >> df2 <- recursive_funlp() >> >> return(df2) >> >> } >> >> } >> >> df3 <- recursive_funlp(dataset = dat1, func = funlp2) >> >> df3 >> >> } >> >> >> >> >> >> *From:* DIGHE, NILESH [AG/2362] >> *Sent:* Thursday, December 14, 2017 9:01 AM >> *To:* 'Eric Berger' <ericjber...@gmail.com> >> *Cc:* r-help <r-help@r-project.org> >> *Subject:* RE: [R] help with recursive function >> >> >> >> Eric: Thanks for taking time to look into my problem. Despite of making >> the change you suggested, I am still getting the same error. I am >> wondering if the logic I am using in the stopifnot and if functions is a >> problem. >> >> I like the recursive function to stop whenever the norm_sd column has >> zero values that are above or equal to 1. Below is the calclp function >> after the changes you suggested. >> >> Thanks. Nilesh >> >> >> >> dput(calclp) >> >> function (dataset) >> >> { >> >> dat1 <- funlp1(dataset) >> >> recursive_funlp <- function(dataset = dat1, func = funlp2) { >> >> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% >> >> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% >> >> spread(key = field_rep, value = lp) %>% mutate_at(.vars = >> grep("_", >> >> names(.)), funs(norm = round(scale(.), 3))) >> >> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, sd, na.rm = TRUE), 3) >> >> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", >> names(dat2))], >> >> 1, function(x) { >> >> max(abs(x), na.rm = TRUE) >> >> }), 3) >> >> stopifnot(!(any(dat2$norm_sd >= 1))) >> >> if (!(any(dat2$norm_sd >= 1))) { >> >> df1 <- dat1 >> >> return(df1) >> >> } >> >> else { >> >> df2 <- r
Re: [R] help with recursive function
The message is coming from your stopifnot() condition being met. On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] < nilesh.di...@monsanto.com> wrote: > Hi, I accidently left out few lines of code from the calclp function. > Updated function is pasted below. > > I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is > not TRUE“ > > > > I would appreciate any help. > > Nilesh > > dput(calclp) > > function (dataset) > > { > > dat1 <- funlp1(dataset) > > recursive_funlp <- function(dataset = dat1, func = funlp2) { > > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% > > spread(key = field_rep, value = lp) %>% mutate_at(.vars = > grep("_", > > names(.)), funs(norm = round(scale(.), 3))) > > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, sd, na.rm = TRUE), 3) > > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, function(x) { > > max(abs(x), na.rm = TRUE) > > }), 3) > > data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, > > -norm_sd) %>% separate(key, c("field_rep", "treatment"), > > "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) > > stopifnot(!(any(data1$norm_sd >= 1))) > > if (!(any(data1$norm_sd >= 1))) { > > df1 <- dat1 > > return(df1) > > } > >else { > > df2 <- recursive_funlp() > > return(df2) > > } > > } > > df3 <- recursive_funlp(dataset = dat1, func = funlp2) > > df3 > > } > > > > > > *From:* DIGHE, NILESH [AG/2362] > *Sent:* Thursday, December 14, 2017 9:01 AM > *To:* 'Eric Berger' <ericjber...@gmail.com> > *Cc:* r-help <r-help@r-project.org> > *Subject:* RE: [R] help with recursive function > > > > Eric: Thanks for taking time to look into my problem. Despite of making > the change you suggested, I am still getting the same error. I am > wondering if the logic I am using in the stopifnot and if functions is a > problem. > > I like the recursive function to stop whenever the norm_sd column has zero > values that are above or equal to 1. Below is the calclp function after the > changes you suggested. > > Thanks. Nilesh > > > > dput(calclp) > > function (dataset) > > { > > dat1 <- funlp1(dataset) > > recursive_funlp <- function(dataset = dat1, func = funlp2) { > > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% > > spread(key = field_rep, value = lp) %>% mutate_at(.vars = > grep("_", > > names(.)), funs(norm = round(scale(.), 3))) > > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, sd, na.rm = TRUE), 3) > > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, function(x) { > > max(abs(x), na.rm = TRUE) > > }), 3) > > stopifnot(!(any(dat2$norm_sd >= 1))) > > if (!(any(dat2$norm_sd >= 1))) { > > df1 <- dat1 > > return(df1) > > } > > else { > > df2 <- recursive_funlp() > > return(df2) > > } > > } > > df3 <- recursive_funlp(dataset = dat1, func = funlp2) > > df3 > > } > > > > > > *From:* Eric Berger [mailto:ericjber...@gmail.com <ericjber...@gmail.com>] > > *Sent:* Thursday, December 14, 2017 8:17 AM > *To:* DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> > *Cc:* r-help <r-help@r-project.org> > *Subject:* Re: [R] help with recursive function > > > > My own typo ... whoops ... > > > > !( any(dat2$norm_sd >= 1 )) > > > > > > > > On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger <ericjber...@gmail.com> > wrote: > > You seem to have a typo at this expression (and some others like it) > > > > Namely, you write > > > > any(!dat2$norm_sd) >= 1 > > > > when you possibly meant to write > > > > !
Re: [R] help with recursive function
Hi, I accidently left out few lines of code from the calclp function. Updated function is pasted below. I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is not TRUE“ I would appreciate any help. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, -norm_sd) %>% separate(key, c("field_rep", "treatment"), "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) stopifnot(!(any(data1$norm_sd >= 1))) if (!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: DIGHE, NILESH [AG/2362] Sent: Thursday, December 14, 2017 9:01 AM To: 'Eric Berger' <ericjber...@gmail.com> Cc: r-help <r-help@r-project.org> Subject: RE: [R] help with recursive function Eric: Thanks for taking time to look into my problem. Despite of making the change you suggested, I am still getting the same error. I am wondering if the logic I am using in the stopifnot and if functions is a problem. I like the recursive function to stop whenever the norm_sd column has zero values that are above or equal to 1. Below is the calclp function after the changes you suggested. Thanks. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) stopifnot(!(any(dat2$norm_sd >= 1))) if (!(any(dat2$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: Eric Berger [mailto:ericjber...@gmail.com] Sent: Thursday, December 14, 2017 8:17 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> Subject: Re: [R] help with recursive function My own typo ... whoops ... !( any(dat2$norm_sd >= 1 )) On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> wrote: You seem to have a typo at this expression (and some others like it) Namely, you write any(!dat2$norm_sd) >= 1 when you possibly meant to write !( any(dat2$norm_sd) >= 1 ) i.e. I think your ! seems to be in the wrong place. HTH, Eric On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: Hi, I need some help with running a recursive function. I like to run funlp2 recursively. When I try to run recursive function in another function named "calclp" I get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE". I have never built a recursive function before so having trouble executing it in this case. I would appreciate any help or guidance to resolve this issue. Please see my data and the three functions that I am using below. Please note that calclp is the function I am running and the other two functions are within this calclp function. # code: Test<- calclp(dataset = dat) # calclp function calclp<- function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
Re: [R] help with recursive function
Eric: Thanks for taking time to look into my problem. Despite of making the change you suggested, I am still getting the same error. I am wondering if the logic I am using in the stopifnot and if functions is a problem. I like the recursive function to stop whenever the norm_sd column has zero values that are above or equal to 1. Below is the calclp function after the changes you suggested. Thanks. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) stopifnot(!(any(dat2$norm_sd >= 1))) if (!(any(dat2$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: Eric Berger [mailto:ericjber...@gmail.com] Sent: Thursday, December 14, 2017 8:17 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> Cc: r-help <r-help@r-project.org> Subject: Re: [R] help with recursive function My own typo ... whoops ... !( any(dat2$norm_sd >= 1 )) On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> wrote: You seem to have a typo at this expression (and some others like it) Namely, you write any(!dat2$norm_sd) >= 1 when you possibly meant to write !( any(dat2$norm_sd) >= 1 ) i.e. I think your ! seems to be in the wrong place. HTH, Eric On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: Hi, I need some help with running a recursive function. I like to run funlp2 recursively. When I try to run recursive function in another function named "calclp" I get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE". I have never built a recursive function before so having trouble executing it in this case. I would appreciate any help or guidance to resolve this issue. Please see my data and the three functions that I am using below. Please note that calclp is the function I am running and the other two functions are within this calclp function. # code: Test<- calclp(dataset = dat) # calclp function calclp<- function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) stopifnot(any(!dat2$norm_sd) >= 1) if (any(!dat2$norm_sd) >= 1) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } # funlp1 function funlp1<- function (dataset) { dat2 <- dataset %>% select(field, set, ent_num, rep_num, lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>% unite(field_rep, field, rep_num) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, -norm_sd) %>% separate(key, c("field_rep", "treatment"), "\\.") %>% spr
Re: [R] help with recursive function
My own typo ... whoops ... !( any(dat2$norm_sd >= 1 )) On Thu, Dec 14, 2017 at 3:43 PM, Eric Bergerwrote: > You seem to have a typo at this expression (and some others like it) > > Namely, you write > > any(!dat2$norm_sd) >= 1 > > when you possibly meant to write > > !( any(dat2$norm_sd) >= 1 ) > > i.e. I think your ! seems to be in the wrong place. > > HTH, > Eric > > > On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] < > nilesh.di...@monsanto.com> wrote: > >> Hi, I need some help with running a recursive function. I like to run >> funlp2 recursively. >> When I try to run recursive function in another function named "calclp" I >> get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE". >> >> I have never built a recursive function before so having trouble >> executing it in this case. I would appreciate any help or guidance to >> resolve this issue. Please see my data and the three functions that I am >> using below. >> Please note that calclp is the function I am running and the other two >> functions are within this calclp function. >> >> # code: >> Test<- calclp(dataset = dat) >> >> # calclp function >> >> calclp<- function (dataset) >> >> { >> >> dat1 <- funlp1(dataset) >> >> recursive_funlp <- function(dataset = dat1, func = funlp2) { >> >> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% >> >> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% >> >> spread(key = field_rep, value = lp) %>% mutate_at(.vars = >> grep("_", >> >> names(.)), funs(norm = round(scale(.), 3))) >> >> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, sd, na.rm = TRUE), 3) >> >> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, function(x) { >> >> max(abs(x), na.rm = TRUE) >> >> }), 3) >> >> stopifnot(any(!dat2$norm_sd) >= 1) >> >> if (any(!dat2$norm_sd) >= 1) { >> >> df1 <- dat1 >> >> return(df1) >> >> } >> >> else { >> >> df2 <- recursive_funlp() >> >> return(df2) >> >> } >> >> } >> >> df3 <- recursive_funlp(dataset = dat1, func = funlp2) >> >> df3 >> >> } >> >> >> # funlp1 function >> >> funlp1<- function (dataset) >> >> { >> >> dat2 <- dataset %>% select(field, set, ent_num, rep_num, >> >> lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>% >> >> unite(field_rep, field, rep_num) %>% mutate(field_rep = >> paste(field_rep, >> >> "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% >> >> mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), >> >> 3))) >> >> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, sd, na.rm = TRUE), 3) >> >> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, function(x) { >> >> max(abs(x), na.rm = TRUE) >> >> }), 3) >> >> data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, >> >> -norm_sd) %>% separate(key, c("field_rep", "treatment"), >> >> "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) >> >> df_clean <- with(data1, data1[norm_sd < 1, ]) >> >> datD <- with(data1, data1[norm_sd >= 1, ]) >> >> s <- split(datD, datD$uniqueid) >> >> sdf <- lapply(s, function(x) { >> >> data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, >> >> ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), >> >> x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) >> >> x >> >> }) >> >> sdf2 <- bind_rows(sdf) >> >> all_dat <- bind_rows(df_clean, sdf2) >> >> all_dat >> >> } >> >> >> # funlp2 function >> >> funlp2<-function (dataset) >> >> { >> >> data1 <- dataset >> >> df_clean <- with(data1, data1[norm_sd < 1, ]) >> >> datD <- with(data1, data1[norm_sd >= 1, ]) >> >> s <- split(datD, datD$uniqueid) >> >> sdf <- lapply(s, function(x) { >> >> data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, >> >> ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), >> >> x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) >> >> x >> >> }) >> >> sdf2 <- bind_rows(sdf) >> >> all_dat <- bind_rows(df_clean, sdf2) >> >> all_dat >> >> } >> >> >> # dataset >> dput(dat) >> structure(list(field = c("LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
Re: [R] help with recursive function
You seem to have a typo at this expression (and some others like it) Namely, you write any(!dat2$norm_sd) >= 1 when you possibly meant to write !( any(dat2$norm_sd) >= 1 ) i.e. I think your ! seems to be in the wrong place. HTH, Eric On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] < nilesh.di...@monsanto.com> wrote: > Hi, I need some help with running a recursive function. I like to run > funlp2 recursively. > When I try to run recursive function in another function named "calclp" I > get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE". > > I have never built a recursive function before so having trouble executing > it in this case. I would appreciate any help or guidance to resolve this > issue. Please see my data and the three functions that I am using below. > Please note that calclp is the function I am running and the other two > functions are within this calclp function. > > # code: > Test<- calclp(dataset = dat) > > # calclp function > > calclp<- function (dataset) > > { > > dat1 <- funlp1(dataset) > > recursive_funlp <- function(dataset = dat1, func = funlp2) { > > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% > > spread(key = field_rep, value = lp) %>% mutate_at(.vars = > grep("_", > > names(.)), funs(norm = round(scale(.), 3))) > > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, sd, na.rm = TRUE), 3) > > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, function(x) { > > max(abs(x), na.rm = TRUE) > > }), 3) > > stopifnot(any(!dat2$norm_sd) >= 1) > > if (any(!dat2$norm_sd) >= 1) { > > df1 <- dat1 > > return(df1) > > } > > else { > > df2 <- recursive_funlp() > > return(df2) > > } > > } > > df3 <- recursive_funlp(dataset = dat1, func = funlp2) > > df3 > > } > > > # funlp1 function > > funlp1<- function (dataset) > > { > > dat2 <- dataset %>% select(field, set, ent_num, rep_num, > > lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>% > > unite(field_rep, field, rep_num) %>% mutate(field_rep = > paste(field_rep, > > "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% > > mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), > > 3))) > > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, sd, na.rm = TRUE), 3) > > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, function(x) { > > max(abs(x), na.rm = TRUE) > > }), 3) > > data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, > > -norm_sd) %>% separate(key, c("field_rep", "treatment"), > > "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) > > df_clean <- with(data1, data1[norm_sd < 1, ]) > > datD <- with(data1, data1[norm_sd >= 1, ]) > > s <- split(datD, datD$uniqueid) > > sdf <- lapply(s, function(x) { > > data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, > > ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), > > x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) > > x > > }) > > sdf2 <- bind_rows(sdf) > > all_dat <- bind_rows(df_clean, sdf2) > > all_dat > > } > > > # funlp2 function > > funlp2<-function (dataset) > > { > > data1 <- dataset > > df_clean <- with(data1, data1[norm_sd < 1, ]) > > datD <- with(data1, data1[norm_sd >= 1, ]) > > s <- split(datD, datD$uniqueid) > > sdf <- lapply(s, function(x) { > > data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, > > ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), > > x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) > > x > > }) > > sdf2 <- bind_rows(sdf) > > all_dat <- bind_rows(df_clean, sdf2) > > all_dat > > } > > > # dataset > dput(dat) > structure(list(field = c("LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "OL01", > "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", > "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", > "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", > "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", > "OL01",
[R] help with recursive function
Hi, I need some help with running a recursive function. I like to run funlp2 recursively. When I try to run recursive function in another function named "calclp" I get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE". I have never built a recursive function before so having trouble executing it in this case. I would appreciate any help or guidance to resolve this issue. Please see my data and the three functions that I am using below. Please note that calclp is the function I am running and the other two functions are within this calclp function. # code: Test<- calclp(dataset = dat) # calclp function calclp<- function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) stopifnot(any(!dat2$norm_sd) >= 1) if (any(!dat2$norm_sd) >= 1) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } # funlp1 function funlp1<- function (dataset) { dat2 <- dataset %>% select(field, set, ent_num, rep_num, lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>% unite(field_rep, field, rep_num) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, -norm_sd) %>% separate(key, c("field_rep", "treatment"), "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) df_clean <- with(data1, data1[norm_sd < 1, ]) datD <- with(data1, data1[norm_sd >= 1, ]) s <- split(datD, datD$uniqueid) sdf <- lapply(s, function(x) { data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) x }) sdf2 <- bind_rows(sdf) all_dat <- bind_rows(df_clean, sdf2) all_dat } # funlp2 function funlp2<-function (dataset) { data1 <- dataset df_clean <- with(data1, data1[norm_sd < 1, ]) datD <- with(data1, data1[norm_sd >= 1, ]) s <- split(datD, datD$uniqueid) sdf <- lapply(s, function(x) { data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) x }) sdf2 <- bind_rows(sdf) all_dat <- bind_rows(df_clean, sdf2) all_dat } # dataset dput(dat) structure(list(field = c("LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1",