[R] avoid losing data.frame attributes on cbind()

Liviu Andronic landronimirc at gmail.com
Fri Apr 19 21:03:10 CEST 2013


Dear Arun,


On Fri, Apr 19, 2013 at 6:30 PM, arun <smartpink111 at yahoo.com> wrote:
> Dear Liviu,
> May be you can use the method mentioned in the link:
> http://stackoverflow.com/questions/10404224/how-to-delete-a-row-from-a-data-frame-without-losing-the-attributes
>
This approach is still much too complicated (and as you mentioned
off-list, it also removes variable names).

For now I've settled with the following function:
append.data.frame <- function(x, values){
    stopifnot(is.data.frame(x))
    stopifnot(is.data.frame(values))
    for(i in names(values)) x[ , i]  <- values[ , i]
    return(x)
}

> Xa <- iris
> label(Xa, self=T) <- "Some df label"
> attributes(Xa)$label
[1] "Some df label"
> Xb <- round(iris[,1:2])
> names(Xb) <- c("var1",'var2')
> Xc <- append.data.frame(Xa, Xb)
> attributes(Xc)$label
[1] "Some df label"

This won't work if there is only one variable to be added (i.e. a
vector). For now this function works, but I would really rather find a
nicer solution to this.

Thanks,
Liviu


> str(Xa)
> #'data.frame':    150 obs. of  5 variables:
> # $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
> # $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
> # $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
> # $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
> # $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> # - attr(*, "label")= chr "Some df label"
> Xc<- cbind(Xa,Xb)
>
> as.data.frame.avector <-as.data.frame.vector `[.avector` <- function(x,i,...) {
>    r <- NextMethod("[")
>    mostattributes(r) <- attributes(x)
>    r
>  }
>
>
> mostattributes(Xc)<- attributes(Xa)
>  str(Xc)
> #'data.frame':    150 obs. of  7 variables:
> # $ : num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
> # $ : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
> # $ : num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
> # $ : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
> # $ : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> # $ : num  5 5 5 5 5 5 5 5 4 5 ...
> # $ : num  4 3 3 3 4 4 3 3 3 3 ...
> # - attr(*, "label")= chr "Some df label"
> A.K.
>
>
>
>
> ----- Original Message -----
> From: Liviu Andronic <landronimirc at gmail.com>
> To: arun <smartpink111 at yahoo.com>
> Cc: R help <r-help at r-project.org>
> Sent: Friday, April 19, 2013 8:13 AM
> Subject: Re: [R] avoid losing data.frame attributes on cbind()
>
> Dear Arun,
>
>
> On Tue, Apr 16, 2013 at 10:45 PM, arun <smartpink111 at yahoo.com> wrote:
>> Another method would be:
>> Xc<- Xa
>>  Xc$var1<-NA; Xc$var2<- NA
>> Xc[]<- append(as.list(Xa),as.list(Xb))
>>
> Unfortunately this is still too convoluted and error prone. And
> mutate() doesn't work for me, as I generate the new vars elsewhere. So
> is there no other clean way to append several new variables to a data
> frame, without losing its attributes?
>
> Regards,
> Liviu
>
>
>
>
>
>> str(Xc)
>> #'data.frame':    150 obs. of  7 variables:
>> # $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
>> # $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
>> # $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
>> # $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
>> # $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
>> # $ var1        : num  5 5 5 5 5 5 5 5 4 5 ...
>> # $ var2        : num  4 3 3 3 4 4 3 3 3 3 ...
>> # - attr(*, "label")= chr "Some df label"
>> A.K.
>>
>>
>>
>> ----- Original Message -----
>> From: arun <smartpink111 at yahoo.com>
>> To: Liviu Andronic <landronimirc at gmail.com>
>> Cc: R help <r-help at r-project.org>
>> Sent: Tuesday, April 16, 2013 2:40 PM
>> Subject: Re: [R] avoid losing data.frame attributes on cbind()
>>
>> HI,
>> Not sure if this helps:
>> library(plyr)
>> res<-mutate(Xa,var1=round(Sepal.Length),var2=round(Sepal.Width))
>> str(res)
>> #'data.frame':    150 obs. of  7 variables:
>> # $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
>> # $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
>> # $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
>> # $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
>> # $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
>> # $ var1        : num  5 5 5 5 5 5 5 5 4 5 ...
>> # $ var2        : num  4 3 3 3 4 4 3 3 3 3 ...
>>  #- attr(*, "label")= chr "Some df label"
>> A.K.
>>
>>
>>
>> ----- Original Message -----
>> From: Liviu Andronic <landronimirc at gmail.com>
>> To: r-help <r-help at stat.math.ethz.ch>
>> Cc:
>> Sent: Tuesday, April 16, 2013 2:24 PM
>> Subject: [R] avoid losing data.frame attributes on cbind()
>>
>> Dear all,
>> How should I add several variables to a data frame without losing the
>> attributes of the df? Consider the following:
>>> require(Hmisc)
>>> Xa <- iris
>>> label(Xa, self=T) <- "Some df label"
>>> str(Xa)
>> 'data.frame':    150 obs. of  5 variables:
>> $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
>> $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
>> $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
>> $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
>> $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1
>> 1 1 1 1 1 1 ...
>> - attr(*, "label")= chr "Some df label"
>>> Xb <- round(iris[,1:2])
>>> names(Xb) <- c("var1",'var2')
>>> Xc <- cbind(Xa, Xb)
>>> #the attribute is now gone
>>> str(Xc)
>> 'data.frame':    150 obs. of  7 variables:
>> $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
>> $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
>> $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
>> $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
>> $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1
>> 1 1 1 1 1 1 ...
>> $ var1        : num  5 5 5 5 5 5 5 5 4 5 ...
>> $ var2        : num  4 3 3 3 4 4 3 3 3 3 ...
>>
>>
>> In such cases, when I want to plug some variables from 2nd df into the
>> 1st df, how should I proceed without losing the attributes of the 1st
>> data frame. And, if possible, I'm looking for something nicer than:
>> for(i in names(Xb)) Xa[ , i] <- Xb[ , i]
>>
>> 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.
>
>
>
> --
> 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
>



-- 
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



More information about the R-help mailing list