I'm trying to write a function that has as an input argument, a column of dataframes that are called iteratively in the function.
Example shown below: Writing a function called iter, which has 2 inputs: 1) the list of dataframes 2) the name of the column that both df1 and df2 contains
iter <- function (dflist, columnname) {
for (df in dflist){
df[,bla:=cut(columnname, etc)]
lm(...data=df)
etc
}
}
E.g:
dflist = list(df1,df2)
and df1 and df2 both contain a column called col1
I want to write a function such that when I type in
iter(dflist,col1)
I get df[,bla:=cut(col1, etc)]
However, whenever I run it now, it gives this error - "object 'col1' not found.
I've tried passing in col1 as a list and use get(columnname), but to no avail:
iter <- function (dflist, columnname) {
for (df in dflist){
df[,bla:=cut(get(columnname), etc)]
lm(...data=df)
etc
}
}
iter(dflist,'col1')
But I get the same error