[R] Characters and Numeric Values in One Matrix
th50 at leicester.ac.uk
Tue Jul 8 11:30:55 CEST 2003
Matrices can only be made of values of the same type, so you can't them here.
data.frame is the right track. ?data.frame states:
Character variables passed to `data.frame' are converted to factor
columns unless protected by `I'. It also applies to adding columns
to a data frame.
> family2 <- data.frame(I(names),ages)
 "Marge" "Lisa" "Homer" "Bart" "Maggie"
Research Associate in Medical Statistics
University of Leicester
Department of Epidemiology and Public Health
22-28 Princess Road West
Tel +44 116 252-5410
Fax +44 116 252-5423
Division of Medicine for the Elderly
Department of Medicine
The Glenfield Hospital
Tel +44 116 256-3643
Fax +44 116 232-2976
> -----Original Message-----
> From: Rau, Roland [mailto:Rau at demogr.mpg.de]
> Sent: 08 July 2003 10:13
> To: 'r-help at stat.math.ethz.ch'
> Subject: [R] Characters and Numeric Values in One Matrix
> Dear R-Users,
> I want to ask a question for a colleague of mine. He wants to put a
> character vector and a numeric vector into one matrix and
> still have the old
> character and numeric type for the respective columns.
> Unfortunately, I am just starting using R and I could not help him.
> Is there an easy and straightforward way to do this in R?
> Maybe a little example facilitates understanding our problem:
> names <- c("Marge", "Lisa", "Homer", "Bart", "Maggie")
> ages <- c(38,10,41,8,1)
> Now he wants to have 2 columns in a matrix which should look
> like this:
> "Marge" 38
> "Lisa" 10
> "Homer" 41
> "Bart" 8
> "Maggie" 1
> I thought about using either:
> family1 <- matrix(c(names, ages), ncol=2, byrow=FALSE)
> family2 <- data.frame(names,ages)
> but this simply transformed either the numeric into character values
> (family1) or the character values into factor levels (family2)
> Anyone here who can give us some advice on this?
> We are using R 1.7.0 on Windows NT.
> R-help at stat.math.ethz.ch mailing list
More information about the R-help