White.Denis@epamail.epa.gov
2004-Feb-06 21:57 UTC
[R] column names in matrix vs. data frame in R 1.8
Is the difference in behavior below, introduced in 1.8, inconsistent or, at least, undesirable? I couldn't find this in the NEWS. On the one hand,> a <- matrix (1:4, nrow=2) > a <- data.frame (a) > names (a) <- c("break","next") > names (a)[1] "break" "next" On the other,> a <- matrix (1:4, nrow=2) > dimnames(a) <- list (1:2, c("break","next")) > a <- data.frame (a) > names(a)[1] "break." "next." thanks, Denis
White.Denis at epamail.epa.gov writes:> Is the difference in behavior below, introduced in 1.8, inconsistent or, > at least, undesirable? I couldn't find this in the NEWS. > > On the one hand, > > > a <- matrix (1:4, nrow=2) > > a <- data.frame (a) > > names (a) <- c("break","next") > > names (a) > [1] "break" "next" > > On the other, > > > a <- matrix (1:4, nrow=2) > > dimnames(a) <- list (1:2, c("break","next")) > > a <- data.frame (a) > > names(a) > [1] "break." "next."Works fine if you don't use keywords as column names> a <- matrix (1:4, nrow=2) > dimnames(a) <- list (1:2, c("foo","bar")) > b <- data.frame(a) > names(b)[1] "foo" "bar" The difference in the result for your example has to do with an extra step in the second case to obtain a legitimate name that can be used with the $ operator. R generates a syntax error for a$break but not for a$break.
White.Denis@epamail.epa.gov
2004-Feb-09 17:27 UTC
[R] column names in matrix vs. data frame in R 1.8
> White.Denis at epamail.epa.gov writes: > > > Is the difference in behavior below, introduced in 1.8, inconsistentor,> > at least, undesirable? I couldn't find this in the NEWS. > > > > On the one hand, > > > > > a <- matrix (1:4, nrow=2) > > > a <- data.frame (a) > > > names (a) <- c("break","next") > > > names (a) > > [1] "break" "next" > > > > On the other, > > > > > a <- matrix (1:4, nrow=2) > > > dimnames(a) <- list (1:2, c("break","next")) > > > a <- data.frame (a) > > > names(a) > > [1] "break." "next." > > Works fine if you don't use keywords as column names > > > a <- matrix (1:4, nrow=2) > > dimnames(a) <- list (1:2, c("foo","bar")) > > b <- data.frame(a) > > names(b) > [1] "foo" "bar" > > The difference in the result for your example has to do with an extra > step in the second case to obtain a legitimate name that can be used > with the $ operator. R generates a syntax error for > > a$break > > but not for > > a$break.Ok, I'll regard it as an inconsistency that the conversion of dimnames to data frame column names changes reserved words to legitimate names but direct assignment doesn't.
White.Denis@epamail.epa.gov
2004-Feb-17 16:31 UTC
[R] column names in matrix vs. data frame in R 1.8
...> > Ok, I'll regard it as an inconsistency that the conversion ofdimnames> > to data frame column names changes reserved words to legitimatenames> > but direct assignment doesn't. > > It's not inconsistent. data.frame has an argument `check.names' to > control the behaviour on *creating* a data frame, and you didn'tconsult> the documentation. Using the function names<- on the list underlyingthe> data frame does not know or care it is applied to a data frame.After thinking about this, I guess I wonder why names<- shouldn't have the argument 'check.names' and/or check the class of its main argument. Why offer protection in one situation and not another?