I have two questions about data frames:
(1) How can one extract a simple matrix
from a data frame? I tried
Matrixfromdf = function (frame,without=1)
{a=frame[colnames(frame)[-without]]
v=unlist(a,use.names=F)
matrix(v,ncol=ncol(a))}
but it works well only for without=1,
perhaps also because the function in (2)
gives probably a different meaning to
the first column.
(2) How does one define a void data frame
with only column names but no values?
I tried this indirect way:
# Void df with titles from ...
Newvoid = function (...)
{a=c(...); m=length(a)
titles=paste(a,collapse=' ')
conn=textConnection(titles)
tab=read.table(conn,header=T)
close(conn); tab}
Thanks
Josef Eschgf??ller
--
Josef Eschgf??ller
Dipartimento Matematico
Universita' di Ferrara
http://felix.unife.it
> I have two questions about data frames: > > (1) How can one extract a simple matrix > from a data frame? I tried > > Matrixfromdf = function (frame,without=1) > {a=frame[colnames(frame)[-without]] > v=unlist(a,use.names=F) > matrix(v,ncol=ncol(a))} > > but it works well only for without=1, > perhaps also because the function in (2) > gives probably a different meaning to > the first column.?as.matrix data(iris) as.matrix(iris[,-5]) # numeric as.matrix(iris[,-1]) # character> (2) How does one define a void data frame > with only column names but no values? > I tried this indirect way: > > # Void df with titles from ... > Newvoid = function (...) > {a=c(...); m=length(a) > titles=paste(a,collapse=' ') > conn=textConnection(titles) > tab=read.table(conn,header=T) > close(conn); tab}Basically, you have to create the sctruture by providing one observation This will allow you to specify the classes of the variables.> data.frame(list(x=1,y="character"))[-1,][1] x y <0 rows> (or 0-length row.names) Eric
Perhaps this works for creating a new void dataframe:
Newvoid = function (...)
{a=c(...); m=length(a)
initial=matrix(rep(NA,m),byrow=T,ncol=m)
tab=data.frame(initial)
colnames(tab)=a; subset(tab,F)}
Josef Eschgf??ller
--
Josef Eschgf??ller
Dipartimento Matematico
Universita' di Ferrara
http://felix.unife.it
> From: Eric Lecoutre > > > I have two questions about data frames: > > > > (1) How can one extract a simple matrix > > from a data frame? I tried > > > > Matrixfromdf = function (frame,without=1) > > {a=frame[colnames(frame)[-without]] > > v=unlist(a,use.names=F) > > matrix(v,ncol=ncol(a))} > > > > but it works well only for without=1, > > perhaps also because the function in (2) > > gives probably a different meaning to > > the first column. > > > ?as.matrix > data(iris) > as.matrix(iris[,-5]) # numeric > as.matrix(iris[,-1]) # characterdata.matrix() might be a safer choice...> > (2) How does one define a void data frame > > with only column names but no values? > > I tried this indirect way: > > > > # Void df with titles from ... > > Newvoid = function (...) > > {a=c(...); m=length(a) > > titles=paste(a,collapse=' ') > > conn=textConnection(titles) > > tab=read.table(conn,header=T) > > close(conn); tab} > > Basically, you have to create the sctruture by providing one > observation > This will allow you to specify the classes of the variables. > > > data.frame(list(x=1,y="character"))[-1,] > [1] x y > <0 rows> (or 0-length row.names)If the columns are all numerics, you can create a matrix with the appriate column names and 0 rows, then coerce to data frame: emptyData <- function(varNames) { m <- matrix(numeric(0), ncol=length(varNames), dimnames=list(NULL, varNames)) as.data.frame(m) } Yet another possibility is to construct the 0-row data frame directly by giving 0-length named arguments to data.frame(). Andy> > Eric > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >