sum date different frames based on column names



  • I really need your help to finish an analysis. I have the following tables (date 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 work I need the three tables to have the columns of name 0, 0.5 and 1. I thought about adding all the tables with a mold table containing the three columns and values all equal to zero, but still couldn't solve this. You think that's the solution? How can I do that? Thank you guys!!



  • 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)))

    Thank you anyway


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2