sum different data frames based on column names

Asked

Viewed 60 times

1

I really need your help to finish an analysis. have the following tables (data frames):

df1<-data.frame(c(0,1,1,0),c(1,0,0,1),c(1,1,0,0))
colnames(df1)<-c("0","0.5","1")
df2<-data.frame(c(1,1,1,0),c(0,1,0,0))
colnames(df2)<-c("0","0.5")
df3<-data.frame(c(1,1,1,1),c(0,0,0,1))
colnames(df3)<-c("0.5","1")

understand that for the analysis to work I need the three tables to have the columns of name 0, 0.5 and 1. I thought of summing all the tables with a template table containing the three columns and all values equal to zero, but I still could not solve this. Do you think this is the solution? How can I do this? Personal thank you!!

1 answer

3

Sorry friends, it was very easy

molde<-data.frame(c(0,0,0,0),c(0,0,0,0),c(0,0,0,0))
colnames(molde)<-c("0","0.5","1")

melted <- cbind(df2,molde)
aggr <- as.data.frame(do.call(cbind, by(t(melted),INDICES=names(melted),FUN=colSums)))

anyway thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.