R Users: I'm wondering: Why does my logical vector becomes a numeric vector when stuffed into a data frame? How do I change this so that it's retained as a logical data type? I've tried a couple of things but to no avail. Here's my example code: # Exercise 4-1 in Non-Detects and Data Analysis. Dennis Helsel. 2005. # "Create two new variables in the Interval Endpoints format, StartCu and EndCu, # that will contain the same information given by the current variables library(NADA) data(CuZn) names(CuZn) StartCu <- ifelse(CuZn$CuCen == "TRUE", 0, CuZn$Cu) EndCu <- CuZn$Cu CuIEP = data.frame(cbind(Cu = CuZn$Cu, CuCen = CuZn$CuCen, StartCu, EndCu)) class(CuZn$CuCen) #returns "logical" class(CuIEP$CuCen) #returns "numeric" CuIEP2 = data.frame(cbind(Cu = CuZn$Cu, CuCen = as.logical(CuZn$CuCen), StartCu, EndCu)) class(CuIEP2$CuCen) #returns "numeric" CuIEP3 = data.frame(cbind(Cu = CuZn$Cu, CuCen = I(CuZn$CuCen), StartCu, EndCu)) class(CuIEP3$CuCen) #returns "numeric" I think that I might be missing something fairly fundamental about data coercion in R. ... would love to figure this out. Any assistance would be appreciated. Thanks much, Matt Findley
Does ?I() help? See ? I() Regards Aval On Wed, Sep 3, 2008 at 9:30 AM, <Matthew.Findley at ch2m.com> wrote:> R Users: > > I'm wondering: > > Why does my logical vector becomes a numeric vector when stuffed into a data frame? How do I change this so that it's retained as a logical data type? I've tried a couple of things but to no avail. > > Here's my example code: > > # Exercise 4-1 in Non-Detects and Data Analysis. Dennis Helsel. 2005. > > # "Create two new variables in the Interval Endpoints format, StartCu and EndCu, > # that will contain the same information given by the current variables > > library(NADA) > data(CuZn) > names(CuZn) > > StartCu <- ifelse(CuZn$CuCen == "TRUE", 0, CuZn$Cu) > EndCu <- CuZn$Cu > > CuIEP = data.frame(cbind(Cu = CuZn$Cu, CuCen = CuZn$CuCen, StartCu, EndCu)) > > class(CuZn$CuCen) > #returns "logical" > class(CuIEP$CuCen) > #returns "numeric" > > CuIEP2 = data.frame(cbind(Cu = CuZn$Cu, CuCen = as.logical(CuZn$CuCen), StartCu, EndCu)) > class(CuIEP2$CuCen) > #returns "numeric" > > CuIEP3 = data.frame(cbind(Cu = CuZn$Cu, CuCen = I(CuZn$CuCen), StartCu, EndCu)) > class(CuIEP3$CuCen) > #returns "numeric" > > I think that I might be missing something fairly fundamental about data coercion in R. ... would love to figure this out. > > Any assistance would be appreciated. > > Thanks much, > > Matt Findley > > ______________________________________________ > R-help at 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. >
The problem is your misuse of cbind. You want CuIEP <- data.frame(Cu = CuZn$Cu, CuCen = CuZn$CuCen, StartCu, EndCu) cbind() created a matrix, as there were numeric inputs the matrix has to be numeric. I've absolutely no idea where you got the idea to use cbind here, but it is helpdful to test each step to see where the conversion occurred, rather than assume your guess is correct. On Tue, 2 Sep 2008, Matthew.Findley at ch2m.com wrote:> R Users: > > I'm wondering: > > Why does my logical vector becomes a numeric vector when stuffed into a data frame? How do I change this so that it's retained as a logical data type? I've tried a couple of things but to no avail. > > Here's my example code: > > # Exercise 4-1 in Non-Detects and Data Analysis. Dennis Helsel. 2005. > > # "Create two new variables in the Interval Endpoints format, StartCu and EndCu, > # that will contain the same information given by the current variables > > library(NADA) > data(CuZn) > names(CuZn) > > StartCu <- ifelse(CuZn$CuCen == "TRUE", 0, CuZn$Cu) > EndCu <- CuZn$Cu > > CuIEP = data.frame(cbind(Cu = CuZn$Cu, CuCen = CuZn$CuCen, StartCu, EndCu)) > > class(CuZn$CuCen) > #returns "logical" > class(CuIEP$CuCen) > #returns "numeric" > > CuIEP2 = data.frame(cbind(Cu = CuZn$Cu, CuCen = as.logical(CuZn$CuCen), StartCu, EndCu)) > class(CuIEP2$CuCen) > #returns "numeric" > > CuIEP3 = data.frame(cbind(Cu = CuZn$Cu, CuCen = I(CuZn$CuCen), StartCu, EndCu)) > class(CuIEP3$CuCen) > #returns "numeric" > > I think that I might be missing something fairly fundamental about data coercion in R. ... would love to figure this out. > > Any assistance would be appreciated. > > Thanks much, > > Matt Findley > > ______________________________________________ > R-help at 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. >-- 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595