[R] fill 0-row data.frame with 1 line of NAs

William Dunlap wdunlap at tibco.com
Wed Jul 11 21:56:15 CEST 2012


Why does one want to replace a zero-row data.frame
with a one-row data.frame of NA's?  Unless this is for
an external program that cannot handle zero-row inputs,
this suggests that there is an unnecessary limitation (i.e.,
a bug) in the R code that uses this data.frame.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of arun
> Sent: Wednesday, July 11, 2012 10:03 AM
> To: Liviu Andronic
> Cc: R help
> Subject: Re: [R] fill 0-row data.frame with 1 line of NAs
> 
> Hi,
> 
> Try this:
>  .xa<-iris[1,][rep(NA,length(iris),1),]
> .xa
> #   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
> #NA           NA          NA           NA          NA    <NA>
> #or
> 
> .xb<-iris[1,][rep(NA,ncol(iris),1),]
>  .xb
> #   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
> #NA           NA          NA           NA          NA    <NA>
> 
> 
> A.K.
> 
> 
> ----- Original Message -----
> From: Liviu Andronic <landronimirc at gmail.com>
> To: "r-help at r-project.org Help" <r-help at r-project.org>
> Cc:
> Sent: Tuesday, July 10, 2012 9:15 AM
> Subject: [R] fill 0-row data.frame with 1 line of NAs
> 
> Dear all
> Is there a simpler method to achieve the following: When I obtain an
> empty data.frame after subsetting, I need for it to contain one line
> of NAs. Here's a dummy example:
> > (.xb <- iris[ iris$Species=='zz', ])
> [1] Sepal.Length Sepal.Width  Petal.Length Petal.Width  Species
> <0 rows> (or 0-length row.names)
> > dim(.xb)
> [1] 0 5
> > (.xa <- data.frame(matrix(rep(NA, ncol(.xb)), 1)))
>   X1 X2 X3 X4 X5
> 1 NA NA NA NA NA
> > names(.xa) <- names(.xb)
> > (.xb <- .xa)
>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
> 1           NA          NA           NA          NA      NA
> 
> 
> The solution I came up with is way too convoluted. Anything simpler? Regards
> Liviu
> 
> 
> --
> Do you know how to read?
> http://www.alienetworks.com/srtest.cfm
> http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
> Do you know how to write?
> http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list