I need a function which adds a new column (with constant values) to a dataframe df. My attempt so far is this:
f = function(df, col.name, col.value){
df$col.name = col.value
print(df)
}
A typical input would be:
f(df, "New column", NA)
This would give me a new column with value NA, however, it would be named col.name.
Any help much appreciated.
R has in-built functions for this sort of thing, namely
$<-for assigning a single column of data.As @MatthewLundberg says in the comment below, you could assign this to your new function if you want to avoid the funky function name: