r - compare column names and make new table -


i want compare 2 columnnames 2 data frames , create new table matched columnnames original data frames.

a<-data.frame(a1=c(1,2,3,4,5),a2=c(2,3,4,5,6),b1=c(3,4,5,6,7),c1=c(4,5,6,7,8)) b<-data.frame(c1=c(10,20,30,40,50),b1=c(20,30,40,50,60),d1=c(30,40,50,60,70)) 

i want make new dataframe adding a+b when column names (e.g. 'c1' etc.) match. e.g. new dataframe named 'c' based on above data: c=data.frame(c1=c(10+4,20+5,30+6,40+7,50+8), b1=c(20+3,30+4,40+5,50+6,60+7))

my original dataframe larger above, using 'for (i in 1:10)'. works when both columnnames in same order in both dataframes.

thanks on advance help.

as.data.frame(lapply(intersect(names(a), names(b)),                function(name) a[name] + b[name])) 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -