retain class after merge

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

retain class after merge

Heinz Tuechler
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,


## 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 ...",
               "Spatial Statistics", "Stochastic Simulation",
               "Interactive Data Analysis",
               "An Introduction to R"), = 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"))

[1] "character"
 > sessionInfo()
R version 2.13.1 Patched (2011-08-08 r56671)
Platform: i386-pc-mingw32/i386 (32-bit)

[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

[hidden email] mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.