[R] fill 0-row data.frame with 1 line of NAs
Rui Barradas
ruipbarradas at sapo.pt
Tue Jul 10 20:05:53 CEST 2012
Hello,
Em 10-07-2012 18:59, Peter Ehlers escreveu:
> On 2012-07-10 08:50, Brian Diggs wrote:
>> On 7/10/2012 7:53 AM, Peter Ehlers wrote:
>>> On 2012-07-10 06:57, Rui Barradas wrote:
>>>> Hello,
>>>>
>>>> If you write a function, it becomes less convoluted...
>>>>
>>>>
>>>> empty <- function(x){
>>>> if(NROW(x) == 0){
>>>> y <- rep(NA, NCOL(x))
>>>> names(y) <- names(x)
>>>> y
>>>> }else x
>>>> }
>>>>
>>>> (.xb <- iris[ iris$Species=='zz', ])
>>>> empty(.xb)
>>>
>>> Both this and Liviu's original solution destroy the
>>> factor nature of 'Species' (which may not matter, of
>>> course). How about
>>>
>>> (.xb <- iris[ iris$Species=='zz', ])
>>> .xb <- .xb[1, ] # this probably shouldn't work, but it does.
>>
>> Using NA subscripting seems even better
>
> Yes, you can subset with NA or any real number greater than 1.
>
> Peter Ehlers
>
Good to know, was completely unaware of this indexing possibility.
Rui Barradas
>>
>> empty <- function(x) {
>> if(NROW(x) == 0) {
>> x[NA,]
>> } else {
>> x
>> }
>> }
>>
>> It even preserves the factor nature of things:
>>
>> > empty(iris[iris$Specis=='zz',])
>> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
>> NA NA NA NA NA <NA>
>> > str(empty(iris[iris$Specis=='zz',]))
>> 'data.frame': 1 obs. of 5 variables:
>> $ Sepal.Length: num NA
>> $ Sepal.Width : num NA
>> $ Petal.Length: num NA
>> $ Petal.Width : num NA
>> $ Species : Factor w/ 3 levels "setosa","versicolor",..: NA
>>
>>
>>> ?
>>>
>>> Peter Ehlers
>>>
>>>>
>>>>
>>>> Hope this helps,
>>>>
>>>> Rui Barradas
>>>>
>>>> Em 10-07-2012 14:15, Liviu Andronic escreveu:
>>>>> 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
>>>>>
>>>>>
>>>>
>>>> ______________________________________________
>>>> 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