Dear all,
I'm creating a list (which is a tbl_graph) by a function, and assign the
result to a variable:
subnet_MYSUBNET <- my_function(MYSUBNET)
# MYSUBNET: a tbl_graph
Because there are multiple subnets to create, I can get the names of the
subnets (MYSUBNET1, MYSUBNET2, MYSUBNET3, etc.) from a row in a
dataframe column.
subnet_MYSUBNET <- my_function(datatable$column[i])
Because I know how many subnets to create - nrow(dataframe) I want to
assign the subnets to variables whose names contain the name of the subnet
For this to work I have to assign a variable name which is contained in
an other variable name:
#################
for(i in 1:nrow(datatable)) {
val <- datatable$column[i]
result <- assign(paste("subnet_",? val, sep = "")
result <- my_function(val)
}
this works in bash, but seems not to work in R - I don't succeed at least.
Am I wrong?
Where is my mistake?
Many thanks in advance for any hint.
Wolfgang
Assigning several lists to variables whose names are contained in other variables
5 messages · PIKAL Petr, Ivan Krylov, Wolfgang Grond
Hi I may be wrong but
result <- assign(paste("subnet_", val, sep = "")
here you miss the right parentheses
maybe you intended
assign(result, paste("subnet_", val, sep = ""))
result <- my_function(val)
but here you change (rewrite) the result variable to my_function result So your overall cycle gives you only one result variable containing last value computed by my_function. Cheers Petr
-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Wolfgang Grond
Sent: Friday, April 9, 2021 11:49 AM
To: mailman, r-help <r-help at r-project.org>
Subject: [R] Assigning several lists to variables whose names are contained
in
other variables
Dear all,
I'm creating a list (which is a tbl_graph) by a function, and assign the
result to
a variable:
subnet_MYSUBNET <- my_function(MYSUBNET)
# MYSUBNET: a tbl_graph
Because there are multiple subnets to create, I can get the names of the
subnets (MYSUBNET1, MYSUBNET2, MYSUBNET3, etc.) from a row in a
dataframe column.
subnet_MYSUBNET <- my_function(datatable$column[i])
Because I know how many subnets to create - nrow(dataframe) I want to
assign the subnets to variables whose names contain the name of the subnet
For this to work I have to assign a variable name which is contained in an
other variable name:
#################
for(i in 1:nrow(datatable)) {
val <- datatable$column[i]
result <- assign(paste("subnet_", val, sep = "")
result <- my_function(val)
}
this works in bash, but seems not to work in R - I don't succeed at least.
Am I wrong?
Where is my mistake?
Many thanks in advance for any hint.
Wolfgang
[[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.
Dear Wolfgang, On Fri, 9 Apr 2021 11:48:55 +0200
Wolfgang Grond <grond at numberland.de> wrote:
I want to assign the subnets to variables whose names contain the name of the subnet
Apologies if this sounds too opinionated, but creating variable names from variable values is a FAQ in a different dynamic language: https://perldoc.perl.org/perlfaq7#How-can-I-use-a-variable-as-a-variable-name? Most of the explanation doesn't apply to R, of course, but the main idea here is to use data structures instead of causing (potential, unlikely, but still) conflicts in the variable namespace. What if you create a list of function values instead of just a bunch of variables? results <- list() for(i in 1:nrow(datatable)) { val <- datatable$column[i] results[[as.character(val)]] <- my_function(val) } Or even results <- lapply(setNames(nm = datatable$column), my_function) Wouldn't that be more convenient?
Best regards, Ivan
Dear Ivan, when I try your solution with lapply as below I get the following error message: Error in eval ... : object 'function' not found ( I think anything about my_function is meant). Am 9. April 2021 13:43:57 MESZ schrieb Ivan Krylov <krylov.r00t at gmail.com>:
Dear Wolfgang, On Fri, 9 Apr 2021 11:48:55 +0200 Wolfgang Grond <grond at numberland.de> wrote:
I want to assign the subnets to variables whose names contain the name of the subnet
Apologies if this sounds too opinionated, but creating variable names from variable values is a FAQ in a different dynamic language: https://perldoc.perl.org/perlfaq7#How-can-I-use-a-variable-as-a-variable-name? Most of the explanation doesn't apply to R, of course, but the main idea here is to use data structures instead of causing (potential, unlikely, but still) conflicts in the variable namespace. What if you create a list of function values instead of just a bunch of variables? results <- list() for(i in 1:nrow(datatable)) { val <- datatable$column[i] results[[as.character(val)]] <- my_function(val) } Or even results <- lapply(setNames(nm = datatable$column), my_function) Wouldn't that be more convenient? -- Best regards, Ivan
______________________________________________ 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.
Ivan, same situation when I try your list() solution: Error in eval ... : object 'function' not found ( I think anything about my_function is meant). Am 9. April 2021 13:43:57 MESZ schrieb Ivan Krylov <krylov.r00t at gmail.com>:
Dear Wolfgang, On Fri, 9 Apr 2021 11:48:55 +0200 Wolfgang Grond <grond at numberland.de> wrote:
I want to assign the subnets to variables whose names contain the name of the subnet
Apologies if this sounds too opinionated, but creating variable names from variable values is a FAQ in a different dynamic language: https://perldoc.perl.org/perlfaq7#How-can-I-use-a-variable-as-a-variable-name? Most of the explanation doesn't apply to R, of course, but the main idea here is to use data structures instead of causing (potential, unlikely, but still) conflicts in the variable namespace. What if you create a list of function values instead of just a bunch of variables? results <- list() for(i in 1:nrow(datatable)) { val <- datatable$column[i] results[[as.character(val)]] <- my_function(val) } Or even results <- lapply(setNames(nm = datatable$column), my_function) Wouldn't that be more convenient? -- Best regards, Ivan
______________________________________________ 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.
- 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: grond at numberland.com URL: http://www.numberland.com