Hi, I tried to use mutate from dplyr with a personal function to create a new variable of a data.frame. All examples I found are such as myfunction(x){...}. In my script, the function calls: - one variable coming from the data.frame - variables coming from lists or constant outside the data.frame My function is something like: var1 <- seq(0,20,1) dat <- data.frame(var1) var2 <- c(0,2,4,8) var3 <- 44.44 var4 <- 0.5 Myfunction <- function(var1, var2, var3,var4){ obs <- 0 obs1 <- 0 for (i in 1:length(var2)) { tt <- var2[i] if ( var1 > tt ){ obs1 <- var3 * exp(-var4*var1) } else obs1 <- 0.0 obs <- obs + obs1 } return(obs) } dat2 <- mutate(dat, obs = Myfunction(var1=var1,var2=var2,var3=var3)) Apparently the condition " if ( var1 > tt ) " cannot be evaluated correcty. Could you please give me an advice to write it properly? Best regards Nicolas [[alternative HTML version deleted]]
You are comparing a 21-element vector with a 1-element vector, which gives you 21 answers. Which one of those answers are you actually interested in? I am not sure what you are trying to accomplish here, so cannot offer further advice. On March 30, 2020 6:21:45 AM PDT, SIMON Nicolas <Nicolas.SIMON at ap-hm.fr> wrote:>Hi, > >I tried to use mutate from dplyr with a personal function to create a >new variable of a data.frame. > >All examples I found are such as myfunction(x){...}. > >In my script, the function calls: >- one variable coming from the data.frame >- variables coming from lists or constant outside the data.frame > >My function is something like: > >var1 <- seq(0,20,1) >dat <- data.frame(var1) >var2 <- c(0,2,4,8) >var3 <- 44.44 >var4 <- 0.5 > >Myfunction <- function(var1, var2, var3,var4){ > obs <- 0 > obs1 <- 0 > for (i in 1:length(var2)) { > tt <- var2[i] > if ( var1 > tt ){ > obs1 <- var3 * exp(-var4*var1) > } > else obs1 <- 0.0 > obs <- obs + obs1 > } > return(obs) >} >dat2 <- mutate(dat, obs = Myfunction(var1=var1,var2=var2,var3=var3)) > >Apparently the condition " if ( var1 > tt ) " cannot be evaluated >correcty. > >Could you please give me an advice to write it properly? > > > >Best regards >Nicolas > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
On 2020-03-30 13:21 +0000, SIMON Nicolas wrote:> Could you please give me an advice to write it > properly?Hi! To understand what you wanted to do, I tried to recreate your example in classic R indexing of a data.frame, then converting it to a tibble: var1 <- seq(0, 20, 1) var2 <- c(0, 2, 4, 8) var3 <- 44.44 var4 <- 0.5 dat <- cbind(var1, var2) dat <- as.data.frame(dat) idx <- dat$var1 > dat$var2 dat[idx, "obs1"] <- var3 * exp(-var4 * dat[idx, "var1"]) dat[!idx, "obs1"] <- 0 dat <- tibble::as_tibble(dat) dat Apparently there is a dplyr::if_else function you can use with dplyr::mutate like you wanted to end up in the same place: var1 <- seq(0, 20, 1) var2 <- c(0, 2, 4, 8) var3 <- 44.44 var4 <- 0.5 dat <- cbind(var1, var2) dat <- as.data.frame(dat) dat <- tibble::as_tibble(dat) dat <- dplyr::mutate(.data=dat, obs=dplyr::if_else( var1>var2, var3*exp(-var4 * var1), 0) ) dat Might this be close to what you were looking to do? /Rasmus
On 2020-03-30 13:21 +0000, SIMON Nicolas wrote: | Apparently the condition " if ( var1 > tt ) " | cannot be evaluated correcty. | | Could you please give me an advice to write it | properly? Hi! To understand what you wanted to do, I tried to recreate your example in classic R indexing of a data.frame, then converting it to a tibble: var1 <- seq(0, 20, 1) var2 <- c(0, 2, 4, 8) var3 <- 44.44 var4 <- 0.5 dat <- cbind(var1, var2) dat <- as.data.frame(dat) idx <- dat$var1 > dat$var2 dat[idx, "obs1"] <- var3 * exp(-var4 * dat[idx, "var1"]) dat[!idx, "obs1"] <- 0 dat <- tibble::as_tibble(dat) dat Apparently there is a dplyr::if_else function you can use with dplyr::mutate like you wanted to end up in the same place: var1 <- seq(0, 20, 1) var2 <- c(0, 2, 4, 8) var3 <- 44.44 var4 <- 0.5 dat <- cbind(var1, var2) dat <- as.data.frame(dat) dat <- tibble::as_tibble(dat) dat <- dplyr::mutate(.data=dat, obs=dplyr::if_else( var1>var2, var3*exp(-var4 * var1), 0) ) dat Might this be close to what you were looking to do? /Rasmus