Let's say I have a df with many columns and 10 of those columns are named "date1" to "date10"
I want to do something like:
for (x in 1:total_number_of_rows) {
for (y in 1:10) {
column_variable_name <- paste0("date",y)
if (df$column_variable_name[x] <= df$another_date_column[x]) {
*lots more code here* }
}
}
Right now what I'm doing is:
for (x in 1:total_number_of_rows) {
if (df$date1[x] <= df$another_date_column[x]) {
*lots more code here* }
if (df$date2[x] <= df$another_date_column[x]) {
*lots more code here* }
if (df$date3[x] <= df$another_date_column[x]) {
*lots more code here* }
if (df$date4[x] <= df$another_date_column[x]) {
*lots more code here* }
if (df$date5[x] <= df$another_date_column[x]) {
*lots more code here* }
etc...
}
And the "lots more code here" is all the same code each time.
The goal is to not have to copy and paste the code 10 times just because I need to change the variable name in the if statement. The first set of code above doesn't work because it's looking for a column in the dataframe called "column_variable_name". Is there any way to do this? What I'm doing in the second set of code seems unnecessary.