1

I am still getting used with functions. I had a look in environments documentation but I can't figure out how to solve the error. Lets see what I tried until now:

I have a list of documents. Lets suppose it is "core"

library(dplyr)
table_1 <- data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))
table_2 <- data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))

core <- list(table_1, table_2) 

Then, I have to run the function documents_ for each element of the list. This function gives some parameters to execute in another nested function:

documents_ <- function(i) {

  core_processed <- as.data.frame(core[[i]])

  x <- 1:nrow(core_processed)
  y <- 1:ncol(core_processed)

  temp <- sapply(x, function(x) mapply(calc_dens_,x,y))

  return(temp)

}

Inside that, there is the function calc_dens, which is:

calc_dens_ <- function(x, y) {

  core_temp <- core_processed %>%
    filter(X2 == x & X3 == y)

   return(core_temp)
}

Then, for iterate for each element of the list, I tried without success:

calc <- lapply(c(1:2), function(i) documents_(i))

Error in eval(lhs, parent, parent) : object 'core_processed' not found

The calc_dens function doesn't get the results of the documents_ (environment problem. Is there a way to solve this, or another better approach? My function is more complex than this, but the main elements are in this example. Thank you in advance.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • 1
    I think you should include the packages that you used. (`dplyr`?). Also, if I put `calc_dens_` inside `documents_`, it runs fine in my machine. – hpesoj626 Mar 20 '18 at 12:54
  • 2
    You should pass `core_processed` to `calc_dens` explicitly. The function doesn't appear to have access to the variable through the environments available to it. – Roman Luštrik Mar 20 '18 at 13:10
  • 1
    `core_processed` is not a global variable, its scope is limited to only within `documents_` function. See the comment above for a solution. – Dave2e Mar 20 '18 at 13:14
  • You are all correct. Putting the complete function calc_dens_ inside documents_ worked well. – Rodrigo Fileto Mar 20 '18 at 13:33

1 Answers1

0

As the other commenters have said, the problem is that you are referring to a variable, core_processed that is not in scope. You can make it a global variable, but it might be more sensible just to use it in a closure like this:

table_1 <- data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))
table_2 <- data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))
cores <- list(table_1, table_2)

documents_ <- function(core_processed) {
    x <- 1:nrow(core_processed)
    y <- 1:ncol(core_processed)
    calc_dens <- function(x, y) core_processed %>% filter(X2 == x & X3 == y)
    sapply(x, function(x) mapply(calc_dens, x, y))
}

calc <- lapply(cores, documents_)

If cores is a list of data frames, you do not need to to use as.data.frame and since you use lapply, there is no need to apply over indices and then index into the list. So the code I wrote here is simplified but does the same as your code.

I have to wonder, though, is this really what you want? The sapply over x and then mapply over x and y -- where x is the one from the sapply and not the ist you built in documents_ -- looks mighty strange to me.

Thomas Mailund
  • 1,674
  • 10
  • 16
  • That is. The context is the following: The table is a edge list of a network. Each x is the value of affiliation. Each Y is value of a specific year. In the case, I want to filter for each cluster (x), a network measure for each year(y). For example, for the cluster 1, I will calculate the transitivity in each year between 2009:2017, and continues for each cluster inside x. – Rodrigo Fileto Mar 20 '18 at 14:00
  • If you want all combinations of `x` and `y`, then `expand.grid(x, y)` might be a better approach. The `mapply` inside the `sapply` is not doing that. – Thomas Mailund Mar 20 '18 at 14:10
  • Good observation. The code was wrong. Following what is stated here [link](https://stackoverflow.com/questions/35352647/r-apply-on-a-function-with-two-variables), I changed `x` to `Lx <- 1:nrow(core_processed)` and `y` as well. After, the code was changed to `sapply(Lx, function(x) mapply(calc_dens,x,Ly))` – Rodrigo Fileto Mar 20 '18 at 17:28