Yulia Kentieva
Yulia Kentieva

Reputation: 710

Apply function with multiple parameters and output variable

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")?

Upvotes: 0

Views: 486

Answers (1)

Jay Achar
Jay Achar

Reputation: 1271

As @Greg mentions, the purpose of this code isn't clear. However, the question seems to relate to how apply() works so here goes:

Basically, when any of the apply family of functions is used, the user-enetered function (f(), in this case) is applied to the subset of the data produced by apply. So here, you've asked apply to evaluate each row then call f() - the first argument to f() would then be a vector rather than the data frame your function requires.

Here's some functioning code:

col_1 <- c("A", "B", "C")
col_2 <- c("red", "blue", "black")
df <- data.frame(col_1, col_2)
f <- function(x) {
  x[1]
  x[2]
}
apply(df, 1, f)

This generates all of the values of the second column as a vector since x[2] is returned from the function and for each row, will represent the value in the second column.

If you want the arg1 row of results, you could simply use the following:

find_row <- function(df, row) {
  df[row, ]
}
find_row(df, 1)

apply() isn't required. Using a single function makes the code simpler to read and should be faster too.

Upvotes: 1

Related Questions