David Kane <David Kane
2002-Apr-30 15:08 UTC
[R] rbind'ing empty rows in dataframes in 1.4.1 versus 1.5.0
Hi, In 1.4.1, I was able to create extra "empty" rows in a dataframe as so:> x <- data.frame(a = letters[1:3], b = 1:3) > xa b 1 a 1 2 b 2 3 c 3> x[4,]a b NA NA NA> rbind(x, x[4,])a b 1 a 1 2 b 2 3 c 3 NA NA NA> R.version_ platform sparc-sun-solaris2.6 arch sparc os solaris2.6 system sparc, solaris2.6 status major 1 minor 4.1 year 2002 month 01 day 30 language R This does not work in 1.5.0> x <- data.frame(a = letters[1:3], b = 1:3) > xa b 1 a 1 2 b 2 3 c 3> x[4,]a b <NA> <NA> NA> rbind(x, x[4,])Error in if (nrow > 0 && all(ri == 1:ni)) seq(from = nrow + 1, length = ni) else ri : missing value where logical needed> R.version_ platform sparc-sun-solaris2.6 arch sparc os solaris2.6 system sparc, solaris2.6 status major 1 minor 5.0 year 2002 month 04 day 29 language R Note that, contrary to my first guess, this has nothing (?) to do with the new NA in character vectors since I see the same behavior with numeric only dataframes.> x <- data.frame(a = 5:7, b = 1:3) > xa b 1 5 1 2 6 2 3 7 3> x[4,]a b <NA> NA NA> rbind(x, x[4,])Error in if (nrow > 0 && all(ri == 1:ni)) seq(from = nrow + 1, length = ni) else ri : missing value where logical needed I didn't see anything in the NEWS about this change. Could someone please explain what is going one? Any words of wisom about the "right" way to create empty rows in dataframe (which I then fill in variable by variable later in the program) would also be appreciated. Thanks, Dave Kane -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2002-Apr-30 15:40 UTC
[R] rbind'ing empty rows in dataframes in 1.4.1 versus 1.5.0
"David Kane <David Kane" <a296180 at mica.fmr.com> writes:> This does not work in 1.5.0 > > > x <- data.frame(a = letters[1:3], b = 1:3) > > x > a b > 1 a 1 > 2 b 2 > 3 c 3 > > x[4,] > a b > <NA> <NA> NA > > rbind(x, x[4,]) > Error in if (nrow > 0 && all(ri == 1:ni)) seq(from = nrow + 1, length = ni) else ri : > missing value where logical needed > > R.version...> Note that, contrary to my first guess, this has nothing (?) to do with the new > NA in character vectors since I see the same behavior with numeric only > dataframes....> I didn't see anything in the NEWS about this change. Could someone please > explain what is going one? Any words of wisom about the "right" way to create > empty rows in dataframe (which I then fill in variable by variable later in the > program) would also be appreciated.We can't anticipate *everything* that people will try to do... I bet the behaviour has to do with new NA in *rownames*. You could try inserting a meaningful name. It probably counts as a bug:> airquality[as.numeric(NA),]Ozone Solar.R Wind Temp Month Day <NA> NA NA NA NA NA NA> airquality[rep(as.numeric(NA),7),]Ozone Solar.R Wind Temp Month Day NA NA NA NA NA NA NA NA1 NA NA NA NA NA NA NA2 NA NA NA NA NA NA NA3 NA NA NA NA NA NA NA4 NA NA NA NA NA NA NA5 NA NA NA NA NA NA NA6 NA NA NA NA NA NA> airquality[c(1,NA),]Ozone Solar.R Wind Temp Month Day <NA> NA NA NA NA NA NA Notice that it is only when there's a single NA row that we see the problem. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian Ripley
2002-Apr-30 15:44 UTC
[R] rbind'ing empty rows in dataframes in 1.4.1 versus 1.5.0
This is indeed to do with missingness of character strings. Data frames cannot have missing row names, and that's what your code creates. So give your missing (it is definitely not `empty') row a valid row name. On Tue, 30 Apr 2002, David Kane <David Kane wrote:> Hi, > > In 1.4.1, I was able to create extra "empty" rows in a dataframe as so: > > > x <- data.frame(a = letters[1:3], b = 1:3) > > x > a b > 1 a 1 > 2 b 2 > 3 c 3 > > x[4,] > a b > NA NA NA > > rbind(x, x[4,]) > a b > 1 a 1 > 2 b 2 > 3 c 3 > NA NA NA > > R.version > _ > platform sparc-sun-solaris2.6 > arch sparc > os solaris2.6 > system sparc, solaris2.6 > status > major 1 > minor 4.1 > year 2002 > month 01 > day 30 > language R > > This does not work in 1.5.0 > > > x <- data.frame(a = letters[1:3], b = 1:3) > > x > a b > 1 a 1 > 2 b 2 > 3 c 3 > > x[4,] > a b > <NA> <NA> NA > > rbind(x, x[4,]) > Error in if (nrow > 0 && all(ri == 1:ni)) seq(from = nrow + 1, length = ni) else ri : > missing value where logical needed > > R.version > _ > platform sparc-sun-solaris2.6 > arch sparc > os solaris2.6 > system sparc, solaris2.6 > status > major 1 > minor 5.0 > year 2002 > month 04 > day 29 > language R > > Note that, contrary to my first guess, this has nothing (?) to do with the new > NA in character vectors since I see the same behavior with numeric only > dataframes. > > > x <- data.frame(a = 5:7, b = 1:3) > > x > a b > 1 5 1 > 2 6 2 > 3 7 3 > > x[4,] > a b > <NA> NA NA > > rbind(x, x[4,]) > Error in if (nrow > 0 && all(ri == 1:ni)) seq(from = nrow + 1, length = ni) else ri : > missing value where logical needed > > > I didn't see anything in the NEWS about this change. Could someone please > explain what is going one? Any words of wisom about the "right" way to create > empty rows in dataframe (which I then fill in variable by variable later in the > program) would also be appreciated. > > Thanks, > > Dave Kane > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2002-May-06 21:19 UTC
Like, Was: Re: [R] rbind'ing empty rows in dataframes in 1.4.1 versus 1.5.0
"David Kane <David Kane" <a296180 at mica.fmr.com> writes:> > x <- data.frame(a = letters[1:3], b = 1:3) > > x > a b > 1 a 1 > 2 b 2 > 3 c 3 > > x[4,] > a b > NA NA NA > > rbind(x, x[4,]) > a b > 1 a 1 > 2 b 2 > 3 c 3 > NA NA NA....> This does not work in 1.5.0.... Brian fixed this for 1.5.1. However, the whole idea helped me with something that has been bugging me for years: When you want to predict on new data foo <- lm(formula, data=foe) predict(foo, newdata=fie) you have to be careful that foe and fie are compatible, all factors have the same level set, and so forth. This can be quite a pain, so I wanted a "like" function as in fie <- like(foe, sex="F", age=20:25, smoke="1-7") but only after seeing the above way of making "blank" rows, it dawned upon me that such rows have exactly the properties I wanted. Elaborating a little, I got like <- function(X,...){ l <- list(...) n <- max(sapply(l, length)) X <- X[rep(as.numeric(NA),n),] row.names(X) <- seq(length=n) for (nm in names(l)) X[[nm]][] <- l[[nm]] X } A six-liner! Neat, eh? Probably has a good chance of making it into 1.6 once I get around to writing a help page. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._