I want to compare two columnnames from two data frames and create a new table with the matched columnnames from the original data frames.
I want to make a new dataframe adding a+b when the column names (e.g. 'c1' etc.) match. E.g. my new dataframe named 'c' will now be like this based on the above data:
My original dataframe is much larger than the above, so I was using 'for (i in 1:10)'. But it works only when both columnnames are in the same order in both dataframes.
Thanks on advance for your help.
as.data.frame(lapply(intersect(names(a), names(b)), function(name) a[name] + b[name]))
the simpler way I can think is
c <- cbind(b[, which(colnames(b)%in% colnames(a))], a[, which(colnames(a)%in% colnames(b))])
is that what you was looking for?