Hello, I would like to know if this behaviour in R is as expected. I have a data frame 'dat' with column var1 being character (and not factor). Then I create a new column 'var2' by: > dat[,"var2"] <- dat$var1; Column var2 is now a factor. But if I do: > dat$var2 <- dat$var1; Then column var2 is character (and not factor). I don't want to have var2 as factor by doing the first assignment. I tried using 'as.character(dat$var1)' or converting dat[,"var2"] to character by using as.character() after the assignment, but still var2 is treated as factor. Note that I want to use dat[,"var2"] and not dat$var2 because I am doing the assignment within a function by referencing column var2 with a variable whose value is "var2" (e.g. dat[,col2] <- dat$var1, where col2 = "var2"). It is easier and more clear to use the [] operator to reference a data frame column with a variable, rather than using the $ operator with the parse() function. Thanks Daniel Mastropietro -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Daniel Mastropietro writes: > Hello, > > I would like to know if this behaviour in R is as expected. It is as expected in R 1.5.1. Note that the NEWS for R-devel (1.6.0 to be) says: o [[<-.data.frame no longer coerces character replacement values to factor. This is consistent with using $ to replace and with S4. So, as best that I understand the details, the behavior will be changing. There was some discussion of this topic on r-help and r-devel a month or two ago (consisting mostly of my confusion and Brian Ripley's patient explanations). See: http://www.r-project.org/nocvs/mail/r-help/2002/4016.html Or the thread beginning with: http://www.r-project.org/nocvs/mail/r-devel/2002/0777.html Regards, Dave Kane > I have a data frame 'dat' with column var1 being character (and not > factor). Then I create a new column 'var2' by: > > > dat[,"var2"] <- dat$var1; > > Column var2 is now a factor. > > But if I do: > > > dat$var2 <- dat$var1; > > Then column var2 is character (and not factor). > > I don't want to have var2 as factor by doing the first assignment. I tried > using 'as.character(dat$var1)' or converting dat[,"var2"] to character by > using as.character() after the assignment, but still var2 is treated as factor. > > Note that I want to use dat[,"var2"] and not dat$var2 because I am doing > the assignment within a function by referencing column var2 with a variable > whose value is "var2" (e.g. dat[,col2] <- dat$var1, where col2 = "var2"). > It is easier and more clear to use the [] operator to reference a data > frame column with a variable, rather than using the $ operator with the > parse() function. > > Thanks > Daniel Mastropietro > > > - -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ > -- David Kane Geode Capital Management 617-563-0122 david.d.kane at fmr.com -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
This is intentional: use class "AsIs" to avoid the conversion, If you had created the data frame as intended (using I()) this would have worked. Data frames are not designed to contain character columns. For example, dat <- data.frame(x=1:3, y = I(letters[1:3])) dat[, "var2"] <- dat$x is.factor(dat$var2) # FALSE On Tue, 9 Jul 2002, Daniel Mastropietro wrote:> Hello, > > I would like to know if this behaviour in R is as expected. > > I have a data frame 'dat' with column var1 being character (and not > factor). Then I create a new column 'var2' by: > > > dat[,"var2"] <- dat$var1; > > Column var2 is now a factor. > > But if I do: > > > dat$var2 <- dat$var1; > > Then column var2 is character (and not factor). > > I don't want to have var2 as factor by doing the first assignment. I tried > using 'as.character(dat$var1)' or converting dat[,"var2"] to character by > using as.character() after the assignment, but still var2 is treated as factor. > > Note that I want to use dat[,"var2"] and not dat$var2 because I am doing > the assignment within a function by referencing column var2 with a variable > whose value is "var2" (e.g. dat[,col2] <- dat$var1, where col2 = "var2"). > It is easier and more clear to use the [] operator to reference a data > frame column with a variable, rather than using the $ operator with the > parse() function. > > Thanks > Daniel Mastropietro > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hello, I have the following two questions, rergarding: (Output) 1) Is it possible to have the output of a command automatically displayed, without explicitly requesting it after the command, and without using the print() function? For example, I want to see the value of 'a' when I write for instance 'a <- rep(1,10)'. I tried setting the 'echo' option to TRUE but it has no effect on that. (History) 2) Is there a way to selectively recall a previously typed command based on the first characters of the command? I am thinking of something similar to this: if one types 'pl' at the prompt and then preses the up arrow key, only the commands starting with 'pl' will show up in the command line. Thanks Daniel Mastropietro -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._