Subtracting every two columns

How about using base R:

cn <- unique(gsub("\\d", "", colnames(mydata)))[-1]
sapply(cn, function(x) mydata[[paste0(x, 1)]] - mydata[[paste0(x, 2)]] )

You can use this approach for any arbitrary number of groups. For example this would return the row sums across the names with the suffix 1 or 2.:

sapply(cn, function(x) rowSums(mydata[, paste0(x, 1:2)]))

This paste approach could be replaced by regular expressions for more general applications.


You can do something like,

sapply(unique(sub('\\d', '', names(mydata[,-1]))), 
       function(i) Reduce('-', mydata[,-1][,grepl(i, sub('\\d', '', names(mydata[,-1])))]))
#       Mary    Bob  Dylan    Tom  Jane    Sam  Tony   Luke   John    Pam
#[1,] -0.106 -0.335 -0.696  0.284 0.567  0.029 0.303  0.272 -0.274 -0.397
#[2,]  0.723  0.526 -0.119 -0.369 0.530 -0.118 0.308  0.159  0.686  0.313
#[3,]  0.174  0.230 -0.259 -0.454 0.039 -0.383 0.193 -0.028 -0.203  0.255

As per your comment, we can easily sort the columns and then apply the formula above,

sorted.names <- names(mydata)[order(nchar(names(mydata)), names(mydata))]
mydata <- mydata[,sorted.names]

Tags:

Regex

R