Assigning several lists to variables whose names are contained in other variables

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Assigning several lists to variables whose names are contained in other variables

Wolfgang Grond
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

PIKAL Petr
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

Greg Minshall
In reply to this post by Wolfgang Grond
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

Ivan Krylov
In reply to this post by Wolfgang Grond
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

Wolfgang Grond-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

Wolfgang Grond-2
In reply to this post by Ivan Krylov
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

Wolfgang Grond-2
In reply to this post by Greg Minshall
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

Rui Barradas
Hello,

my_function seems to be a function you have defined somewhere in your code.
In your original post you mention it 3 times, this is the first one:


subnet_MYSUBNET <- my_function(MYSUBNET)


So Ivan's and Greg's code should work, they use a function you haven't
posted but is assumed to exist.

Note: if you are more comfortable with for loops than with *apply, I
would rewrite Ivan's for loop solution as


results <- vector("list", length = nrow(datatable))
for(val in datatable$column) {
        results[[as.character(val)]] <- my_function(val)
}


To keep extending a vector or list object in a loop is inefficient, this
creates the list with the right length beforehand.


Hope this helps,

Rui Barradas

Às 13:21 de 09/04/21, Wolfgang Grond escreveu:

> Greg,
>
> here I get the error message:
>
> Error my_function(val) :
>
> cannot find function my_function.
>
> Am 9. April 2021 12:35:40 MESZ schrieb Greg Minshall <[hidden email]>:
>> Wolfgang,
>>
>>> result <- assign(paste("subnet_",  val, sep = "")
>>>
>>> result <- my_function(val)
>>
>> i don't understand why you are twice assigning to =result=.  also, the
>> first assignment doesn't seem well formatted (t's missing a value?).
>>
>> did you mean something like
>>
>> : assign(paste("subnet_",  val, sep = ""), my_function(val))
>>
>> (which i would think should work)?
>>
>> cheers, Greg
>>
>> ______________________________________________
>> [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.
>
>
> -
> Numberland - Dr. Wolfgang Grond
> Diplomphysiker, TQM-Assessor (EFQM)
> Six Sigma Green Belt
> Ingenieurbüro / Engineering Consultancy
> Lohfeld 20, DE-95326 Kulmbach, Germany
> Phone: +49 9221 6919131
> Fax: +49 9221 6919156
> Mail: [hidden email]
> URL: http://www.numberland.com
> [[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: Assigning several lists to variables whose names are contained in other variables

Wolfgang Grond-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

David Winsemius
In reply to this post by Wolfgang Grond-2

On 4/9/21 5:21 AM, Wolfgang Grond wrote:
> Greg,
>
> here I get the error message:
>
> Error my_function(val) :
>
> cannot find function my_function.

I'm guessing that you are following someone else's blog and have failed
one of two things:

- understand that what was meant by the author was that you were assumed
to have a function in mind to use for a programming strategy being
illustrated

- or you were copying and pasting only part of a blog and failed to
paste in the code from above where there was earlier code defining
`my_function`

>
> Am 9. April 2021 12:35:40 MESZ schrieb Greg Minshall <[hidden email]>:
>> Wolfgang,
>>
>>> result <- assign(paste("subnet_",  val, sep = "")
>>>
>>> result <- my_function(val)
>> i don't understand why you are twice assigning to =result=.  also, the
>> first assignment doesn't seem well formatted (t's missing a value?).
>>
>> did you mean something like
>>
>> : assign(paste("subnet_",  val, sep = ""), my_function(val))
>>
>> (which i would think should work)?
>>
>> cheers, Greg
>>
>> ______________________________________________
>> [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.
>
> -
> Numberland - Dr. Wolfgang Grond
> Diplomphysiker, TQM-Assessor (EFQM)
> Six Sigma Green Belt
> Ingenieurbüro / Engineering Consultancy
> Lohfeld 20, DE-95326 Kulmbach, Germany
> Phone: +49 9221 6919131
> Fax: +49 9221 6919156
> Mail: [hidden email]
> URL: http://www.numberland.com
> [[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: Assigning several lists to variables whose names are contained in other variables

Wolfgang Grond-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

Wolfgang Grond-2
In reply to this post by David Winsemius
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

Bert Gunter-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Assigning several lists to variables whose names are contained in other variables

Rui Barradas
In reply to this post by Wolfgang Grond-2
Hello,

I believe that the point we are missing is that datatable$column stores
the *names* of the graphs, not the graph objects themselves. So in the
loop the objects must be retrieved with mget() or get().

First create a reproducible example.



library(tidygraph)

my_function <- function(g){
   stopifnot(inherits(g, "igraph"))
   g %>% mutate(centrality = centrality_pagerank())
}

MYSUBNET1 <- create_notable('bull')
iris_clust <- hclust(dist(iris[1:4]))
MYSUBNET2 <- as_tbl_graph(iris_clust)
MYSUBNET3 <- play_smallworld(1, 100, 3, 0.05)

datatable <- data.frame(column = paste0("MYSUBNET", 1:3))



Now apply the function above to each of the "tbl_graph" objects.

1. Get all objects and apply the function in one instruction. Then
assign the new names.


result <- lapply(mget(datatable$column, envir = .GlobalEnv), my_function)
names(result) <- paste("subnet", datatable$column, sep = "_")


2. Loop through the column with lapply, getting one object and applying
the function one at a time. Then assign the new names.



result2 <- lapply(datatable$column, function(net_name){
   NET <- get(net_name, envir = .GlobalEnv)
   my_function(NET)
})
names(result2) <- paste("subnet", datatable$column, sep = "_")



3. Loop through the column with a for loop, getting one object and
applying the function one at a time. Then assign the new names.



result3 <- vector("list", length = nrow(datatable))
for(i in seq_along(datatable$column)){
   net_name <- datatable$column[i]
   NET <- get(net_name, envir = .GlobalEnv)
   result3[[i]] <- my_function(NET)
}
names(result3) <- paste("subnet", datatable$column, sep = "_")



4. Now check that all 3 solutions give the same result.


identical(result, result2)
#[1] TRUE
identical(result, result3)
#[1] TRUE


Is this it?

Rui Barradas


Às 17:23 de 09/04/21, Wolfgang Grond escreveu:

> As I wrote before, I calculate tbl_graph objects, which will be joined afterwards. Not too much, the number of graphs to calculate is in the range between 5 to 20.
>
> Further steps are not automated, because they depend on how the single graphs look like, and which of them will be joined.
>
> For this reason I thought it would be nice to have the single tbl_ graph objects stored in variables having the name of the graph.
>
> For this reason I tried to find a better solution instead of assigning each graph by hand:
>
> subnet_MYSUBNET <- my_function(MYSUBNET)
>
> To my understanding it is therefore neccessary to assign the result of the function to a variable whose name consists of a fixed string and the content of a further variable.
>
> That was the intention for me to ask.
>
> Am 9. April 2021 17:22:05 MESZ schrieb David Winsemius <[hidden email]>:
>>
>> On 4/9/21 5:21 AM, Wolfgang Grond wrote:
>>> Greg,
>>>
>>> here I get the error message:
>>>
>>> Error my_function(val) :
>>>
>>> cannot find function my_function.
>>
>> I'm guessing that you are following someone else's blog and have failed
>>
>> one of two things:
>>
>> - understand that what was meant by the author was that you were
>> assumed
>> to have a function in mind to use for a programming strategy being
>> illustrated
>>
>> - or you were copying and pasting only part of a blog and failed to
>> paste in the code from above where there was earlier code defining
>> `my_function`
>>
>>>
>>> Am 9. April 2021 12:35:40 MESZ schrieb Greg Minshall
>> <[hidden email]>:
>>>> Wolfgang,
>>>>
>>>>> result <- assign(paste("subnet_",  val, sep = "")
>>>>>
>>>>> result <- my_function(val)
>>>> i don't understand why you are twice assigning to =result=.  also,
>> the
>>>> first assignment doesn't seem well formatted (t's missing a value?).
>>>>
>>>> did you mean something like
>>>>
>>>> : assign(paste("subnet_",  val, sep = ""), my_function(val))
>>>>
>>>> (which i would think should work)?
>>>>
>>>> cheers, Greg
>>>>
>>>> ______________________________________________
>>>> [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.
>>>
>>> -
>>> Numberland - Dr. Wolfgang Grond
>>> Diplomphysiker, TQM-Assessor (EFQM)
>>> Six Sigma Green Belt
>>> Ingenieurbüro / Engineering Consultancy
>>> Lohfeld 20, DE-95326 Kulmbach, Germany
>>> Phone: +49 9221 6919131
>>> Fax: +49 9221 6919156
>>> Mail: [hidden email]
>>> URL: http://www.numberland.com
>>> [[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.
>
> [[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.