Reputation: 1659
I have to carry out a number of comparisons, and I was wondering how to automate the overall procedure. Suppose I have only the following variables:
age location serial weight height age_n location_n serial_n weight_n height_n
I want to compare
data[weight != weight_n,c("serial ","weight","weight_n") , drop = F]
for each and every variable. How can I loop over all the variables?
I was trying
for (nam in colnames(df)) {
data['nam' != 'nam'_n,c("serial ",'nam','nam'_n) , drop = F]
}
Upvotes: 0
Views: 1539
Reputation: 52687
Here is an alternate. Note you should only loop through the column names w/o the _n
(hence the grep
in the for
section):
for(nam in colnames(df)[-grep("_n$", colnames(df))]) {
data[
data[[nam]] != data[[paste0(nam, "_n"]],
c("serial", nam, paste0(nam, "_n")
]
}
Upvotes: 3