[R] retain class after merge
Heinz Tuechler
tuechler at gmx.at
Fri Aug 19 13:24:49 CEST 2011
Dear All,
is there a simple way to retain the class attribute of a column, if
merging two data.frames?
When merging the example data.frames form help(merge) I am unable to
keep the class attribute as set before merging (see below).
Two columns are assigned new classes before merge (myclass1,
myclass2), but after merge the resulting column has class "character".
best regards,
Heinz
## use character columns of names to get sensible sort order
authors <- data.frame(
surname = I(c("Tukey", "Venables", "Tierney", "Ripley", "McNeil")),
nationality = c("US", "Australia", "US", "UK", "Australia"),
deceased = c("yes", rep("no", 4)))
books <- data.frame(
name = I(c("Tukey", "Venables", "Tierney",
"Ripley", "Ripley", "McNeil", "R Core")),
title = c("Exploratory Data Analysis",
"Modern Applied Statistics ...",
"LISP-STAT",
"Spatial Statistics", "Stochastic Simulation",
"Interactive Data Analysis",
"An Introduction to R"),
other.author = c(NA, "Ripley", NA, NA, NA, NA,
"Venables & Smith"))
class(authors$surname) <- 'myclass1'
class(books$name) <- 'myclass2'
(m1 <- merge(authors, books, by.x = "surname", by.y = "name"))
class(m1$surname)
[1] "character"
>
> sessionInfo()
R version 2.13.1 Patched (2011-08-08 r56671)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252
[3] LC_MONETARY=German_Switzerland.1252 LC_NUMERIC=C
[5] LC_TIME=German_Switzerland.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
More information about the R-help
mailing list