[R] basic dataframe question

Matthew.Findley at ch2m.com Matthew.Findley at ch2m.com
Wed Sep 3 06:00:17 CEST 2008

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


StartCu <- ifelse(CuZn$CuCen == "TRUE", 0, CuZn$Cu)
EndCu <- CuZn$Cu

CuIEP = data.frame(cbind(Cu = CuZn$Cu, CuCen = CuZn$CuCen, StartCu, EndCu))

#returns "logical"
#returns "numeric"

CuIEP2 = data.frame(cbind(Cu = CuZn$Cu, CuCen = as.logical(CuZn$CuCen), StartCu, EndCu))
#returns "numeric"

CuIEP3 = data.frame(cbind(Cu = CuZn$Cu, CuCen = I(CuZn$CuCen), StartCu, EndCu))
#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

More information about the R-help mailing list