Firstly, I have two dataframe I combined dataframes having same columns but different suffixes using common column removing suffix of the columns. Now I have to calculate difference between each two columns with same column names in a dataframe in R
I tried getting the unique column column names and form that substracting the columns, but common Id column converting to NA.
ffsub[[k]][[i-1]] <- sapply(unique(names(temp)),
function(x) apply(temp[grep(x, names(temp))], 1, function(y) { y[1] - y[2] }))
also tried;
ffsub[[k]][[i-1]] <- sapply(unique(names(temp)[2:21]),
function(x) apply(temp[grep(x, names(temp)[2:21])], 1,
function(y) if ( all(is.na(y)) ) {NA} else { y[1] - y[2] }))
Result should be the difference between cloumns having same col names in dataframe.