Re: [R] data frame "sum"

2 views
Skip to first unread message

arun

unread,
May 23, 2013, 2:30:59 PM5/23/13
to R help
Hi,
ab<- cbind(a,b)
indx<-duplicated(names(ab))|duplicated(names(ab),fromLast=TRUE)
res1<-cbind(ab[!indx],v2=rowSums(ab[indx]))
 res1[,order(as.numeric(gsub("[A-Za-z]","",names(res1)))),]
#v1 v2 v3
#1  3  4  5

#Another example:

a2<- data.frame(v1=c(3,6,7),v2=c(2,4,8))
 b2<- data.frame(v2=c(2,6,7),v3=c(5,4,9))
 ab2<- cbind(a2,b2)
indx<-duplicated(names(ab2))|duplicated(names(ab2),fromLast=TRUE)
res1<-cbind(ab2[!indx],v2=rowSums(ab2[indx]))
 res1[,order(as.numeric(gsub("[A-Za-z]","",names(res1)))),]
#  v1 v2 v3
#1  3  4  5
#2  6 10  4
#3  7 15  9
A.K.


>Dear R expert,
>I have two data frame a and b:
>a <- data.frame(v1=3,v2=2)
>b <- data.frame(v2=2,v3=5)
>
>Is it possible to obtain a new data frame resulting from the sum of the previous df with the 3 variables? namely
>v1,v2,v3
>3,4,5
>
>Thanx,
>Gianandrea

______________________________________________
R-h...@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.
Reply all
Reply to author
Forward
0 new messages