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

arun smartpink111 at yahoo.com
Tue Apr 16 22:45:29 CEST 2013


Hi,
Another method would be:
Xc<- Xa
 Xc$var1<-NA; Xc$var2<- NA
Xc[]<- append(as.list(Xa),as.list(Xb))
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.



More information about the R-help mailing list