Winfried Moser
2013-Jan-31 14:34 UTC
[R] use name (not values!) of a dataframe inside a funktion
Dear Listers, can anyone help me, please. Since several days i try to figure out, how to assign values, vectors, functions etc to variables with dynamically generated names inside of functions. Sometimes I succeed, but the success is rather arbitrary, it seems. up to now i don't fully understand, why things like get, assign, <<- etc do sometimes work, and sometimes not. here's one of my daily examples, i am stuck with: Example 1 does work, but example 2 doesn't? How kann i tell R, that i want it to expand the string "dfb" to "dfb[,2]" inside the function. In the end i want the function to change the second variable of the dataframe dfb permanently to factor (not just inside the function). Thanks in advance! Winfried Example 1: dfa <- data.frame(a=c(1:4),b=c(1:4)) dfa[,2] <- factor(dfa[,2]) is.factor(dfa[,2])>TRUEExample 2: dfb <- data.frame(a=c(1:4),b=c(1:4)) f.fact <- function(x) {x[,2] <<- factor(x[,2])} f.fact(dfb) is.factor(dfb[,2])>FALSEPS: I tried a whole lot of other things like, ... I really don't know where to keep on searching. dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) f.fact <- function(x) {get(x)[,2] <<- factor(get(x)[,2])} f.fact("dfb") is.factor(dfb[,2])> "Object 'x' nicht gefundendfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) f.fact <- function(x) {get(x[,2]) <<- factor(x[,2])} f.fact(dfb) is.factor(dfb[,2])> "Object 'x' nicht gefundendfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) f.fact <- function(x) {get(x)[,2] <<- factor(x[,2])} f.fact(dfb) is.factor(dfb[,2])> "Object 'x' nicht gefundendfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) f.fact <- function(x) {assign(x[,2], factor(x[,2]))} f.fact(dfb) is.factor(dfb[,2])> Ungültiges erstes Argumentdfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) f.fact <- function(x) {quote(x)[,2], factor(x[,2])} f.fact(dfb) is.factor(dfb[,2])> Unerwartetes ','dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) f.fact <- function(x) { name <- paste0(quote(x),"[,2]") assign(name, factor(x[,2]))} f.fact(dfb) is.factor(dfb[,2])> FALSEdfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) f.fact <- function(x) { name <- paste0(get(x),"[,2]") assign(name, factor(x[,2]))} f.fact("dfb") is.factor(dfb[,2])> Falsche Anzahl von Dimensionendfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) f.fact <- function(x) { name <- paste0(x,"[,2]") assign(name, factor(x[,2]))} f.fact("dfb") is.factor(dfb[,2])> Falsche Anzahl von Dimensionenächz ... [[alternative HTML version deleted]]
PIKAL Petr
2013-Feb-01 08:32 UTC
[R] use name (not values!) of a dataframe inside a funktion
Hi Maybe others can help you but here is my comment. I already use R for many years and never used such construction. Objects in global environment shall not be modified by functions it is a bad practice. Imagine you have some data frame you prepared and controlled in many steps and use some function from a package. If this function scrambles object without warning and the result cannot be easily repaired I would be tempted to curse the function with many nasty four letter words. When I want to change some data by a function I use fff <- function(x, no=2) { x[,no]<-factor(x[, no]) x } dfb.f <- fff(dfb) In this case I will end with old object dfb and new object dfb.f. Of course it is possible to use dfb <- fff(dfb) and in this case dfb object is changed Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Winfried Moser > Sent: Thursday, January 31, 2013 3:35 PM > To: r-help > Subject: [R] use name (not values!) of a dataframe inside a funktion > > Dear Listers, > > can anyone help me, please. > > Since several days i try to figure out, how to assign values, vectors, > functions etc to variables with dynamically generated names inside of > functions. > Sometimes I succeed, but the success is rather arbitrary, it seems. up > to now i don't fully understand, why things like get, assign, <<- etc > do sometimes work, and sometimes not. > > here's one of my daily examples, i am stuck with: Example 1 does work, > but example 2 doesn't? > How kann i tell R, that i want it to expand the string "dfb" to > "dfb[,2]" > inside the function. > In the end i want the function to change the second variable of the > dataframe dfb permanently to factor (not just inside the function). > > Thanks in advance! > > Winfried > > > Example 1: > dfa <- data.frame(a=c(1:4),b=c(1:4)) > dfa[,2] <- factor(dfa[,2]) > is.factor(dfa[,2]) > >TRUE > > Example 2: > dfb <- data.frame(a=c(1:4),b=c(1:4)) > f.fact <- function(x) {x[,2] <<- factor(x[,2])} > f.fact(dfb) > is.factor(dfb[,2]) > >FALSE > > > PS: I tried a whole lot of other things like, ... > I really don't know where to keep on searching. > > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) {get(x)[,2] <<- factor(get(x)[,2])} > f.fact("dfb") > is.factor(dfb[,2]) > > "Object 'x' nicht gefunden > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) {get(x[,2]) <<- factor(x[,2])} > f.fact(dfb) > is.factor(dfb[,2]) > > "Object 'x' nicht gefunden > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) {get(x)[,2] <<- factor(x[,2])} > f.fact(dfb) > is.factor(dfb[,2]) > > "Object 'x' nicht gefunden > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) {assign(x[,2], factor(x[,2]))} > f.fact(dfb) > is.factor(dfb[,2]) > > Ung?ltiges erstes Argument > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) {quote(x)[,2], factor(x[,2])} > f.fact(dfb) > is.factor(dfb[,2]) > > Unerwartetes ',' > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) { > name <- paste0(quote(x),"[,2]") > assign(name, factor(x[,2]))} > f.fact(dfb) > is.factor(dfb[,2]) > > FALSE > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) { > name <- paste0(get(x),"[,2]") > assign(name, factor(x[,2]))} > f.fact("dfb") > is.factor(dfb[,2]) > > Falsche Anzahl von Dimensionen > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) { > name <- paste0(x,"[,2]") > assign(name, factor(x[,2]))} > f.fact("dfb") > is.factor(dfb[,2]) > > Falsche Anzahl von Dimensionen > > ?chz ... > > [[alternative HTML version deleted]]
Greg Snow
2013-Feb-01 16:53 UTC
[R] use name (not values!) of a dataframe inside a funktion
It is strongly discouraged in R to have functions that change data values in the global workspace (or any location other than their local environment). The usual procedure in R is to have your function return a modified version of the object and the user then decides what to do with it. They can assign it back to the same original object so that there is still only one copy and it has changed (but the user made that decision, not the programmer), or they can save it to a different name and not lose the original. If you really want to change the original copy (and there are sometimes when the exception to the rule makes sense) then you can either use environments (which don't copy on modify) or use macros instead of functions. Given your examples I would look at the macro approach first. There is a 'defmacro' function in the 'gtools' package and the reference on the help page for 'defmacro' leads to the original R news (now R Journal) article describing the use of macros in R (definitely read this if you are considering this approach). On Thu, Jan 31, 2013 at 7:34 AM, Winfried Moser <winfried.moser@gmail.com>wrote:> Dear Listers, > > can anyone help me, please. > > Since several days i try to figure out, how to assign values, vectors, > functions etc to variables with dynamically generated names inside of > functions. > Sometimes I succeed, but the success is rather arbitrary, it seems. up to > now i don't fully understand, why things like get, assign, <<- etc do > sometimes work, and sometimes not. > > here's one of my daily examples, i am stuck with: Example 1 does work, but > example 2 doesn't? > How kann i tell R, that i want it to expand the string "dfb" to "dfb[,2]" > inside the function. > In the end i want the function to change the second variable of the > dataframe dfb permanently to factor (not just inside the function). > > Thanks in advance! > > Winfried > > > Example 1: > dfa <- data.frame(a=c(1:4),b=c(1:4)) > dfa[,2] <- factor(dfa[,2]) > is.factor(dfa[,2]) > >TRUE > > Example 2: > dfb <- data.frame(a=c(1:4),b=c(1:4)) > f.fact <- function(x) {x[,2] <<- factor(x[,2])} > f.fact(dfb) > is.factor(dfb[,2]) > >FALSE > > > PS: I tried a whole lot of other things like, ... > I really don't know where to keep on searching. > > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) {get(x)[,2] <<- factor(get(x)[,2])} > f.fact("dfb") > is.factor(dfb[,2]) > > "Object 'x' nicht gefunden > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) {get(x[,2]) <<- factor(x[,2])} > f.fact(dfb) > is.factor(dfb[,2]) > > "Object 'x' nicht gefunden > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) {get(x)[,2] <<- factor(x[,2])} > f.fact(dfb) > is.factor(dfb[,2]) > > "Object 'x' nicht gefunden > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) {assign(x[,2], factor(x[,2]))} > f.fact(dfb) > is.factor(dfb[,2]) > > Ungültiges erstes Argument > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) {quote(x)[,2], factor(x[,2])} > f.fact(dfb) > is.factor(dfb[,2]) > > Unerwartetes ',' > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) { > name <- paste0(quote(x),"[,2]") > assign(name, factor(x[,2]))} > f.fact(dfb) > is.factor(dfb[,2]) > > FALSE > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) { > name <- paste0(get(x),"[,2]") > assign(name, factor(x[,2]))} > f.fact("dfb") > is.factor(dfb[,2]) > > Falsche Anzahl von Dimensionen > > dfb <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4)) > f.fact <- function(x) { > name <- paste0(x,"[,2]") > assign(name, factor(x[,2]))} > f.fact("dfb") > is.factor(dfb[,2]) > > Falsche Anzahl von Dimensionen > > ächz ... > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]