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

Liviu Andronic landronimirc at gmail.com
Fri Apr 19 14:13:38 CEST 2013


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



More information about the R-help mailing list