[S] assignments to data frames

Jeff Longmate (jlongmate@smtplink.coh.org)
Fri, 13 Nov 98 10:42:24 -0800

I'd like to convert to and from factors (mostly from) inside a data frame,
and I noticed that assigning to [ lets me change to character but not to factor,
while assigning to [[ lets me change to factor but not to character,
yet assigning to a named column (e.g. df$a) lets me do either.
Can anyone offer an explanation (or rationalization) for this behavior, or how I
can predict what will happen with other conversions?
This makes me feel like I'm missing something.

(Splus version 4.0 release 3 for Windows)

> df <- data.frame(a=letters[1:5], n=1:5)
> data.class(df[[1]])
[1] "factor"
> df[[1]] <- as.character(df[[1]]) # [[ <- doesn't do the job.
> data.class(df[[1]])
[1] "factor"
> df[1] <- as.character(df[[1]]) # [ <- does.
> data.class(df[[1]])
[1] "character"
> df[2] <- as.factor(df[[2]]) # now try to convert the numeric variable
> data.class(df[[2]])
[1] "character"
> # How'd that happen?
> # Going back to numeric and trying the other way:
> df[2] <- as.numeric(df[[2]])
> data.class(df[[2]])
[1] "numeric"
> df[[2]] <- as.factor(df[[2]])
> data.class(df[[2]])
[1] "factor"

Assigning to named list elements works like I'd expect:
> df <- data.frame(a=letters[1:5], n=1:5)
> data.class(df$a)
[1] "factor"
> df$a <- as.character(df$a)
> data.class(df$a)
[1] "character"
> df$n <- as.factor(df$n)
> data.class(df$n)
[1] "factor"

Using a name inside brackets, e.g. df[["a"]], gives the same results as with
numeric indices.

The help page on Subscript.data.frame doesn't offer a clue, except to say
"it will generally be much more natural and less error-prone to construct the

replacement data as a data frame with the appropriate replacement variables in

the columns." That would be easier advice to take if I() worked on a
character matrix, or if there was an as.is flag analogous to the one
in read.table.

Jeffrey A. Longmate, Ph.D. Phone: 626-359-8111 ext.2478
Department of Biostatistics Fax: 626-301-8802
City of Hope National Medical Center E-mail: jlongmat@coh.org
and Beckman Research Institute
1500 East Duarte Road
Duarte CA, USA 91010-3000

This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news