[R] dataframes without data
Wolfgang Koller
koller2 at fgr.wu-wien.ac.at
Fri Nov 19 10:39:47 CET 1999
Hi!
Yesterday I posted the following problem:
> There seems to be some inconsistency in the following behaviour:
>
> > MYDATA <- data.frame(b=c("A","B","C"),x=c(1,2,3))
> > MYDATA
> b x
> 1 A 1
> 2 B 2
> 3 C 3
> > MYDATAnew <- MYDATA[0:0,]
> > MYDATAnew
> [1] b x
> <0 rows> (or 0-length row.names)
> > MYDATAnew <- rbind(MYDATA[0:0,],MYDATA)
> > MYDATAnew
> b x
> 1 1 1
> 2 2 2
> 3 3 3
>
> Does R forget the type of the columns of a data.frame when the
> data.frame contains no data??
>From the following experiment I conclude, that the answer to this
question is "no" and that the problem lies in rbind() (maybe this is a
bug?):
> AAA <- data.frame(b=c("A","B","C"),x=c(1,2,3))
> str(AAA)
`data.frame': 3 obs. of 2 variables:
$ b: Factor class ; 3 levels: A,B: 1 2 3
..- attr(*, "class")= chr "factor"
$ x: num 1 2 3
> BBB <- AAA[0:0,]
> str(BBB)
`data.frame': 0 obs. of 2 variables:
$ b: Factor class ; 3 levels: A,B:
..- attr(*, "class")= chr "factor"
$ x: num
> CCC <- rbind(BBB,AAA)
> str(CCC)
`data.frame': 3 obs. of 2 variables:
$ b: int 1 2 3
$ x: num 1 2 3
> DDD <- rbind(AAA,BBB)
Error: missing value where logical needed
Do I do something wrong, when I intend to use dataframes in that way?
I should also mention, that Olivier Houix (thanks!) suggested to me to
do:
> AAA <- data.frame(b=c("A","B","C"),x=c(1,2,3))
> CCC <- rbind(AAA[1:1,],AAA)
But then I have to cut away the first row again:
> CCC <-CCC[-1,]
> CCC
b x
2 A 1
3 B 2
4 C 3
This has a side effect on the row-numbers. Also, I hoped that there
would be a more elegant solution.
Thanks for suggestions, that help me understand better the basic
concepts of R!
Wolfgang Koller
----------------------------------------------------------
Wolfgang Koller, koller2 at fgr.wu-wien.ac.at
Forschungsinstitut fuer Europafragen
Wirtschaftsuniversitaet Wien
Althanstrasse 39-45, 1090 Wien, Austria
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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