[R] Creating a dataframe from several numeric and character vectors

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 6 20:31:53 CET 2001


On Tue, 6 Nov 2001, Sven Garbade wrote:

> Hi all,
>
> I want to combine some numeric vectors and one character vector to a
> dataframe and did this:
>
>     dat <- cbind(type, key,response, st)
>     dimnames(dat) <- list(NULL, labels)
>     dat <- data.frame(as.numeric(dat$"type"),
> as.numeric(dat$"key"),
> as.numeric(dat$"response"), dat$"st")
>
> where st is the character vector. I got this error:
>
> Error in "names<-.default"(*tmp*, value = vnames) :
> 	names attribute must be the same length as the vector
>
> If I only type
>
>     dat <- data.frame(dat)
>
> then all variables are factors. I need all variables as numeric except
> st. How can I do it?

dat <- data.frame(type=as.numeric(dat$"type"),, key =
as.numeric(dat$"key"), response = as.numeric(dat$"response"), st=dat$"st")

It needs to be given suitable names.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list