When customizing last line, the code stops working

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

When customizing last line, the code stops working

Frank S.
Dear all,


The function I present works perfectly when I run it as written (that is, leaving NEW LINE as commented). However, when

I try to run the same function via this mentioned line (and therefore commenting LAST LINE) R gives an error message:
Error in FUN(X[[i]], ...) : object 'dt_sp_1' not found. I do not understand why I don't get the same result.


Thanks in advance for any help!


Frank S.


all.sp <- function(age.u, open, close) {

require(data.table)
dt <- data.table( id = c(rep(1, 2), 2:4, rep(5, 2)),
   sex = as.factor(rep(c(0, 1), c(3, 4))),
   fborn =  as.Date(c("1935-07-25", "1935-07-25", "1939-07-23", "1943-10-05",
                      "1944-01-01", "1944-09-07", "1944-09-07")) )

sp <- seq(open, close, by = "year")
dt_sp <- list()
for (i in 1:length(sp)) {
  vp <- as.POSIXlt(c(as.Date("1000-01-01"), sp))
  vp$year <- vp$year - age.u
  dt.cut <- as.numeric(cut(x = as.POSIXlt(dt$fborn), breaks = vp, right = TRUE, include.lowest = TRUE))
  dt_sp[i] <- split(dt, factor(dt.cut, i))
  dt_sp[[i]] <- data.table(dt_sp[[i]])[, entry_sp := sp[i]]
  assign(paste0("dt_sp_", 1:length(sp))[i], dt_sp[[i]])
  }

  union <- rbind(dt_sp_1, dt_sp_2, dt_sp_3, dt_sp_4)     # LAST LINE: IT WORKS


  # I TRY TO CUSTOMIZE LAST LINE, BUT THEN CODE STOPS WORKING
  # union <- do.call(rbind, lapply(paste0("dt_sp_", 1:length(sp)), get))     # NEW LINE
}

# Example:
result <- all.sp(age.u = 65, open = as.Date("2007-01-01"), close = as.Date("2010-05-01"))

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: When customizing last line, the code stops working

Fowler, Mark-2
Hi Frank,

Maybe just the quotes?

paste0("dt_sp_", 1:length(sp))
[1] "dt_sp_1" "dt_sp_2" "dt_sp_3" "dt_sp_4"
noquote(paste0("dt_sp_", 1:length(sp)))
 [1] dt_sp_1 dt_sp_2 dt_sp_3 dt_sp_4


-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Frank S.
Sent: November 3, 2016 9:30 AM
To: [hidden email]
Subject: [R] When customizing last line, the code stops working

Dear all,


The function I present works perfectly when I run it as written (that is, leaving NEW LINE as commented). However, when

I try to run the same function via this mentioned line (and therefore commenting LAST LINE) R gives an error message:
Error in FUN(X[[i]], ...) : object 'dt_sp_1' not found. I do not understand why I don't get the same result.


Thanks in advance for any help!


Frank S.


all.sp <- function(age.u, open, close) {

require(data.table)
dt <- data.table( id = c(rep(1, 2), 2:4, rep(5, 2)),
   sex = as.factor(rep(c(0, 1), c(3, 4))),
   fborn =  as.Date(c("1935-07-25", "1935-07-25", "1939-07-23", "1943-10-05",
                      "1944-01-01", "1944-09-07", "1944-09-07")) )

sp <- seq(open, close, by = "year")
dt_sp <- list()
for (i in 1:length(sp)) {
  vp <- as.POSIXlt(c(as.Date("1000-01-01"), sp))
  vp$year <- vp$year - age.u
  dt.cut <- as.numeric(cut(x = as.POSIXlt(dt$fborn), breaks = vp, right = TRUE, include.lowest = TRUE))
  dt_sp[i] <- split(dt, factor(dt.cut, i))
  dt_sp[[i]] <- data.table(dt_sp[[i]])[, entry_sp := sp[i]]
  assign(paste0("dt_sp_", 1:length(sp))[i], dt_sp[[i]])
  }

  union <- rbind(dt_sp_1, dt_sp_2, dt_sp_3, dt_sp_4)     # LAST LINE: IT WORKS


  # I TRY TO CUSTOMIZE LAST LINE, BUT THEN CODE STOPS WORKING
  # union <- do.call(rbind, lapply(paste0("dt_sp_", 1:length(sp)), get))     # NEW LINE
}

# Example:
result <- all.sp(age.u = 65, open = as.Date("2007-01-01"), close = as.Date("2010-05-01"))

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: When customizing last line, the code stops working

Duncan Murdoch-2
In reply to this post by Frank S.
On 03/11/2016 8:29 AM, Frank S. wrote:
> Dear all,
>
>
> The function I present works perfectly when I run it as written (that is, leaving NEW LINE as commented). However, when
>
> I try to run the same function via this mentioned line (and therefore commenting LAST LINE) R gives an error message:
> Error in FUN(X[[i]], ...) : object 'dt_sp_1' not found. I do not understand why I don't get the same result.

You aren't specifying the "envir" argument to "get", so it defaults to
the environment from which you called get, and that's the evaluation
frame of lapply() (or maybe of do.call()).

Write the last line as

union <- do.call(rbind, lapply(paste0("dt_sp_", 1:length(sp)), get, envir = environment()))


and it works.

Duncan Murdoch

>
>
> Thanks in advance for any help!
>
>
> Frank S.
>
>
> all.sp <- function(age.u, open, close) {
>
> require(data.table)
> dt <- data.table( id = c(rep(1, 2), 2:4, rep(5, 2)),
>     sex = as.factor(rep(c(0, 1), c(3, 4))),
>     fborn =  as.Date(c("1935-07-25", "1935-07-25", "1939-07-23", "1943-10-05",
>                        "1944-01-01", "1944-09-07", "1944-09-07")) )
>
> sp <- seq(open, close, by = "year")
> dt_sp <- list()
> for (i in 1:length(sp)) {
>    vp <- as.POSIXlt(c(as.Date("1000-01-01"), sp))
>    vp$year <- vp$year - age.u
>    dt.cut <- as.numeric(cut(x = as.POSIXlt(dt$fborn), breaks = vp, right = TRUE, include.lowest = TRUE))
>    dt_sp[i] <- split(dt, factor(dt.cut, i))
>    dt_sp[[i]] <- data.table(dt_sp[[i]])[, entry_sp := sp[i]]
>    assign(paste0("dt_sp_", 1:length(sp))[i], dt_sp[[i]])
>    }
>
>    union <- rbind(dt_sp_1, dt_sp_2, dt_sp_3, dt_sp_4)     # LAST LINE: IT WORKS
>
>
>    # I TRY TO CUSTOMIZE LAST LINE, BUT THEN CODE STOPS WORKING
>    # union <- do.call(rbind, lapply(paste0("dt_sp_", 1:length(sp)), get))     # NEW LINE
> }
>
> # Example:
> result <- all.sp(age.u = 65, open = as.Date("2007-01-01"), close = as.Date("2010-05-01"))
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: When customizing last line, the code stops working

Frank S.
Many thanks to Mark and Duncan for your help!


As Duncan Murdoch indicated in his answer, it was a matter of envir argument from function do.call.


Thank you!


Frank S.

________________________________
De: Duncan Murdoch <[hidden email]>
Enviado: jueves, 3 de noviembre de 2016 14:41:26
Para: Frank S.; [hidden email]
Asunto: Re: [R] When customizing last line, the code stops working

On 03/11/2016 8:29 AM, Frank S. wrote:
> Dear all,
>
>
> The function I present works perfectly when I run it as written (that is, leaving NEW LINE as commented). However, when
>
> I try to run the same function via this mentioned line (and therefore commenting LAST LINE) R gives an error message:
> Error in FUN(X[[i]], ...) : object 'dt_sp_1' not found. I do not understand why I don't get the same result.

You aren't specifying the "envir" argument to "get", so it defaults to
the environment from which you called get, and that's the evaluation
frame of lapply() (or maybe of do.call()).

Write the last line as

union <- do.call(rbind, lapply(paste0("dt_sp_", 1:length(sp)), get, envir = environment()))


and it works.

Duncan Murdoch
>
>
[[elided Hotmail spam]]

>
>
> Frank S.
>
>
> all.sp <- function(age.u, open, close) {
>
> require(data.table)
> dt <- data.table( id = c(rep(1, 2), 2:4, rep(5, 2)),
>     sex = as.factor(rep(c(0, 1), c(3, 4))),
>     fborn =  as.Date(c("1935-07-25", "1935-07-25", "1939-07-23", "1943-10-05",
>                        "1944-01-01", "1944-09-07", "1944-09-07")) )
>
> sp <- seq(open, close, by = "year")
> dt_sp <- list()
> for (i in 1:length(sp)) {
>    vp <- as.POSIXlt(c(as.Date("1000-01-01"), sp))
>    vp$year <- vp$year - age.u
>    dt.cut <- as.numeric(cut(x = as.POSIXlt(dt$fborn), breaks = vp, right = TRUE, include.lowest = TRUE))
>    dt_sp[i] <- split(dt, factor(dt.cut, i))
>    dt_sp[[i]] <- data.table(dt_sp[[i]])[, entry_sp := sp[i]]
>    assign(paste0("dt_sp_", 1:length(sp))[i], dt_sp[[i]])
>    }
>
>    union <- rbind(dt_sp_1, dt_sp_2, dt_sp_3, dt_sp_4)     # LAST LINE: IT WORKS
>
>
>    # I TRY TO CUSTOMIZE LAST LINE, BUT THEN CODE STOPS WORKING
>    # union <- do.call(rbind, lapply(paste0("dt_sp_", 1:length(sp)), get))     # NEW LINE
> }
>
> # Example:
> result <- all.sp(age.u = 65, open = as.Date("2007-01-01"), close = as.Date("2010-05-01"))
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: When customizing last line, the code stops working

Frank S.
A clarification regarding my previous post for those ineterested in the issue:


Looking carefully at Duncan's solution, the key point would be introducing envir = environment()

as argument of lapply function (the default envir of do.call function is parent.frame() ).

)

 union <- do.call(
               what = rbind,
               args = lapply(
                        X = paste0("dt_sp_", 1:length(sp)),
                        FUN = get,
                       envir = environment()
                       )
 )


________________________________
De: Frank S.
Enviado: viernes, 4 de noviembre de 2016 12:33:11
Para: Duncan Murdoch; [hidden email]; [hidden email]
Asunto: Re: [R] When customizing last line, the code stops working


[[elided Hotmail spam]]


As Duncan Murdoch indicated in his answer, it was a matter of envir argument from function do.call.


Thank you!


Frank S.

________________________________
De: Duncan Murdoch <[hidden email]>
Enviado: jueves, 3 de noviembre de 2016 14:41:26
Para: Frank S.; [hidden email]
Asunto: Re: [R] When customizing last line, the code stops working

On 03/11/2016 8:29 AM, Frank S. wrote:
> Dear all,
>
>
> The function I present works perfectly when I run it as written (that is, leaving NEW LINE as commented). However, when
>
> I try to run the same function via this mentioned line (and therefore commenting LAST LINE) R gives an error message:
> Error in FUN(X[[i]], ...) : object 'dt_sp_1' not found. I do not understand why I don't get the same result.

You aren't specifying the "envir" argument to "get", so it defaults to
the environment from which you called get, and that's the evaluation
frame of lapply() (or maybe of do.call()).

Write the last line as

union <- do.call(rbind, lapply(paste0("dt_sp_", 1:length(sp)), get, envir = environment()))


and it works.

Duncan Murdoch
>
>
[[elided Hotmail spam]]

>
>
> Frank S.
>
>
> all.sp <- function(age.u, open, close) {
>
> require(data.table)
> dt <- data.table( id = c(rep(1, 2), 2:4, rep(5, 2)),
>     sex = as.factor(rep(c(0, 1), c(3, 4))),
>     fborn =  as.Date(c("1935-07-25", "1935-07-25", "1939-07-23", "1943-10-05",
>                        "1944-01-01", "1944-09-07", "1944-09-07")) )
>
> sp <- seq(open, close, by = "year")
> dt_sp <- list()
> for (i in 1:length(sp)) {
>    vp <- as.POSIXlt(c(as.Date("1000-01-01"), sp))
>    vp$year <- vp$year - age.u
>    dt.cut <- as.numeric(cut(x = as.POSIXlt(dt$fborn), breaks = vp, right = TRUE, include.lowest = TRUE))
>    dt_sp[i] <- split(dt, factor(dt.cut, i))
>    dt_sp[[i]] <- data.table(dt_sp[[i]])[, entry_sp := sp[i]]
>    assign(paste0("dt_sp_", 1:length(sp))[i], dt_sp[[i]])
>    }
>
>    union <- rbind(dt_sp_1, dt_sp_2, dt_sp_3, dt_sp_4)     # LAST LINE: IT WORKS
>
>
>    # I TRY TO CUSTOMIZE LAST LINE, BUT THEN CODE STOPS WORKING
>    # union <- do.call(rbind, lapply(paste0("dt_sp_", 1:length(sp)), get))     # NEW LINE
> }
>
> # Example:
> result <- all.sp(age.u = 65, open = as.Date("2007-01-01"), close = as.Date("2010-05-01"))
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.