Re: [R] Code driven data.frame naming question.
Thanks for the reply Jeff. I will play around with your code example and see where it takes me. -Original Message- From: Jeff Newmiller Sent: Monday, April 15, 2019 4:55 PM To: r-help@r-project.org; Fieck, Joe ; R-help@r-project.org Subject: Re: [R] Code driven data.frame naming question. [External Email] While the assign function is in fact the function you are looking for, I would strongly advise that you cease and desist in this endeavour and instead make a list of data frames rather than littering your global environment with many individual data frames. If you have a vector of names of files you can use lapply to read them all into a list of data frames. You can then use the filenames as "names" with which to look up the appropriate data. E.g. dtadir <- "datadir" myfiles <- list.files( dtadir ) dtalist <- lapply( myfiles, function(fn) { read.csv( file.path( dtadir, fn ), stringsAsFactors=FALSE ) } names( dtalist ) <- myfiles str(dtalist$file001.csv) str(dtalist[[ "file001.csv" ]]) str( dtalist[[ 1 ]] ) If the filenames have unusual characters in them then you may have to use back-tick quotes when using the dollar-sign operator. On April 15, 2019 8:50:44 AM PDT, "Fieck, Joe" wrote: >Hello R list. I'm very new. I have what I hope is a simple question >that I have not been able to find a good solution for. >If this is common and clutters anyone's inbox my most sincere apologies >in advance... > >I am trying to use an argument from a function in the name of a >data.frame. But I seem to only be able to reference the arguments from >the right hand side of the <- . > >So something like this. I would just want the function below to create >a new data.frame named "myDataSet". >Done on its own like "myDataSet <- data.frame()" > >myFunction <- function(x){ # suppose x is just a collection with >one value with is the name I want the data.frame create as.. >c("myDataSet") for example > name(x) <- data.frame() >} > >But this of course doesn't work because I have not figured out how to >reference the function argument from the left hand side of the <- . >I have read about the "assign" function but from what I can tell >(granted I'm new to R) the assign function works on the variable names >not data.frame names which is what I need. > >Ultimately what I'm trying accomplish to send a list of character >strings then iterate it and create multiple new data.frames each having >the name of one of the elements of the list passed to the function. > >If there is even a better article out on the web that I'm obviously >missing please orient me. I have struck out so far but I know I may >not be searching correctly either. >Any help at all would be much appreciated... >__ >R-help@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. __ R-help@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.
Re: [R] Code driven data.frame naming question.
While the assign function is in fact the function you are looking for, I would strongly advise that you cease and desist in this endeavour and instead make a list of data frames rather than littering your global environment with many individual data frames. If you have a vector of names of files you can use lapply to read them all into a list of data frames. You can then use the filenames as "names" with which to look up the appropriate data. E.g. dtadir <- "datadir" myfiles <- list.files( dtadir ) dtalist <- lapply( myfiles, function(fn) { read.csv( file.path( dtadir, fn ), stringsAsFactors=FALSE ) } names( dtalist ) <- myfiles str(dtalist$file001.csv) str(dtalist[[ "file001.csv" ]]) str( dtalist[[ 1 ]] ) If the filenames have unusual characters in them then you may have to use back-tick quotes when using the dollar-sign operator. On April 15, 2019 8:50:44 AM PDT, "Fieck, Joe" wrote: >Hello R list. I'm very new. I have what I hope is a simple question >that I have not been able to find a good solution for. >If this is common and clutters anyone's inbox my most sincere apologies >in advance... > >I am trying to use an argument from a function in the name of a >data.frame. But I seem to only be able to reference the arguments from >the right hand side of the <- . > >So something like this. I would just want the function below to create >a new data.frame named "myDataSet". >Done on its own like "myDataSet <- data.frame()" > >myFunction <- function(x){ # suppose x is just a collection with >one value with is the name I want the data.frame create as.. >c("myDataSet") for example > name(x) <- data.frame() >} > >But this of course doesn't work because I have not figured out how to >reference the function argument from the left hand side of the <- . >I have read about the "assign" function but from what I can tell >(granted I'm new to R) the assign function works on the variable names >not data.frame names which is what I need. > >Ultimately what I'm trying accomplish to send a list of character >strings then iterate it and create multiple new data.frames each having >the name of one of the elements of the list passed to the function. > >If there is even a better article out on the web that I'm obviously >missing please orient me. I have struck out so far but I know I may >not be searching correctly either. >Any help at all would be much appreciated... >__ >R-help@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. __ R-help@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.
[R] Code driven data.frame naming question.
Hello R list. I'm very new. I have what I hope is a simple question that I have not been able to find a good solution for. If this is common and clutters anyone's inbox my most sincere apologies in advance... I am trying to use an argument from a function in the name of a data.frame. But I seem to only be able to reference the arguments from the right hand side of the <- . So something like this. I would just want the function below to create a new data.frame named "myDataSet". Done on its own like "myDataSet <- data.frame()" myFunction <- function(x){ # suppose x is just a collection with one value with is the name I want the data.frame create as.. c("myDataSet") for example name(x) <- data.frame() } But this of course doesn't work because I have not figured out how to reference the function argument from the left hand side of the <- . I have read about the "assign" function but from what I can tell (granted I'm new to R) the assign function works on the variable names not data.frame names which is what I need. Ultimately what I'm trying accomplish to send a list of character strings then iterate it and create multiple new data.frames each having the name of one of the elements of the list passed to the function. If there is even a better article out on the web that I'm obviously missing please orient me. I have struck out so far but I know I may not be searching correctly either. Any help at all would be much appreciated... __ R-help@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.
[R] code for [[.data.frame
Hello. I'm currently trying to wrap up data frames into OCaml via OCaml-R, and I'm having trouble with data frame subsetting: # x#column 1;; Erreur dans (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, : l'élément 1 est vide ; la partie de la liste d'arguments de 'is.matrix' en cours d'évaluation était : (i) So I'd like to know what is the code of [[.data.frame. I know how to show the code of functions in R (just typing the name of the function), but I'm having trouble with [[.data.frame, as it has a special syntacting handling. Could someone kindly show me how to display the code of [[.data.frame in the R toploop? All the best, -- Guillaume Yziquel http://yziquel.homelinux.org/ __ R-help@r-project.org mailing list 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.
Re: [R] code for [[.data.frame
`[[.data.frame` and more generally see Rnews Volume 6/4, October 2006 Accessing the Sources. HTH, baptiste 2009/12/13 Guillaume Yziquel guillaume.yziq...@citycable.ch: Hello. I'm currently trying to wrap up data frames into OCaml via OCaml-R, and I'm having trouble with data frame subsetting: # x#column 1;; Erreur dans (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, : l'élément 1 est vide ; la partie de la liste d'arguments de 'is.matrix' en cours d'évaluation était : (i) So I'd like to know what is the code of [[.data.frame. I know how to show the code of functions in R (just typing the name of the function), but I'm having trouble with [[.data.frame, as it has a special syntacting handling. Could someone kindly show me how to display the code of [[.data.frame in the R toploop? All the best, -- Guillaume Yziquel http://yziquel.homelinux.org/ __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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.
Re: [R] code for [[.data.frame
baptiste auguie a écrit : `[[.data.frame` and more generally see Rnews Volume 6/4, October 2006 Accessing the Sources. HTH, baptiste Thank you so much. Indeed, I've been able to look at the source code of the function from the source code of R. But I was quite keen on knowing how to do this from the toploop. Thanks a lot. -- Guillaume Yziquel http://yziquel.homelinux.org/ __ R-help@r-project.org mailing list 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.
Re: [R] code for [[.data.frame
On 12/13/2009 02:42 PM, Guillaume Yziquel wrote: baptiste auguie a écrit : `[[.data.frame` Note that this will only be the source code if you have options keep.source and keep.source.pkgs set to TRUE, and the environment variable R_KEEP_PKG_SOURCE equal to yes. Otherwise you see the code parsed and deparsed, which loses comments and formatting. `[[.data.frame` function(x, ..., exact=TRUE) { ## use in-line functions to refer to the 1st and 2nd ... arguments ## explicitly. Also will check for wrong number or empty args na - nargs() - !missing(exact) if(!all(names(sys.call()) %in% c(, exact))) warning(named arguments other than 'exact' are discouraged) if(na 3L) (function(x, i, exact) if(is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, i, exact=exact))(x, ..., exact=exact) else { col - .subset2(x, ..2, exact=exact) i - if(is.character(..1)) pmatch(..1, row.names(x), duplicates.ok = TRUE) else ..1 .subset2(col, i, exact=exact) } } environment: namespace:base writeLines( deparse( `[[.data.frame` ) ) function (x, ..., exact = TRUE) { na - nargs() - (!missing(exact)) if (!all(names(sys.call()) %in% c(, exact))) warning(named arguments other than 'exact' are discouraged) if (na 3L) (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, i, exact = exact))(x, ..., exact = exact) else { col - .subset2(x, ..2, exact = exact) i - if (is.character(..1)) pmatch(..1, row.names(x), duplicates.ok = TRUE) else ..1 .subset2(col, i, exact = exact) } } Romain and more generally see Rnews Volume 6/4, October 2006 Accessing the Sources. HTH, baptiste Thank you so much. Indeed, I've been able to look at the source code of the function from the source code of R. But I was quite keen on knowing how to do this from the toploop. Thanks a lot. -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/HlX9 : new package : bibtex |- http://tr.im/Gq7i : ohloh `- http://tr.im/FtUu : new package : highlight __ R-help@r-project.org mailing list 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.