Skip to content
Prev 299810 / 398503 Next

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:
[1] Sepal.Length Sepal.Width? Petal.Length Petal.Width? Species
<0 rows> (or 0-length row.names)
[1] 0 5
? X1 X2 X3 X4 X5
1 NA NA NA NA NA
? 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