I am trying to apply a function with two arguments. The first argument is a dataframe, the second is an integer that defines a row of the df.
col_1 <- c("A", "B", "C")
col_2 <- c("red", "blue", "black")
df <- data.frame(col_1, col_2)
f <- function(x, arg1) {
x[arg1, 1]
x[arg1, 2]
}
apply(df, 1, f)
Looks like the second argument is not passed to the function. Here is the error
Error in x[arg1, 1] : incorrect number of dimensions
when I put arg1=1 like this
apply(df, arg1=1, f)
it gives me a FUN error
Error in match.fun(FUN) : argument "FUN" is missing, with no default
the desired output is "A" and "red", i.e. in my real code I need to operate with the values of each row.
I also want to add an output variable to be able to save a plot that I am making in my real analysis in a file. Can I just add an "output" variable in function(x, arg1) and then do apply(df, arg1=1, f, output="output_file")?
f()? As it stands,f()itself will return only the valuex[arg1, 2]: the value of the final statement in the function (in lieu of areturn()statement). Furthermore, this seems to misuse theapply()function. If you want to simply subset rows and columns of yourdf, subscripting is the way to go:df[1, ]ordf[1, 1:2]ordf[1, c("col_1", "col_2")]; or more generallydf[vector_of_row_indices_or_names, vector_of_column_indices_or_names]. In this