[R] data frame "sum"

arun smartpink111 at yahoo.com
Thu May 23 20:30:59 CEST 2013


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



More information about the R-help mailing list