Tim Hampson
Tim Hampson

Reputation: 13

How can I apply a function to every row of a data frame in R when the function requires multiple inputs?

I need to run a chi squared test on data from each row of a data frame in R. So far I have a function that can create a matrix and run the test on the matrix. This is working fine when I manually enter data into the function.

chisquare.table <- function(var1, var2, var3, var4){
  t <- matrix(c(var1, var2, var3, var4), nrow = 2)
  chisq.test(t)

chisquare.table(80, 99920, 85, 99915)

However, what I want to do is apply this function to each row of a data frame such that var1 is row x column 1, var2 is row x column 2, var3 is row x column 3, and var4 is row x column 4.

I've tried a few different ways with the apply() function but I can't find one that allows me to take the data from the row in the way I want. I'd really appreciate any help or advice on this as I haven't found much online about using apply() with multiple inputs.

Upvotes: 1

Views: 170

Answers (1)

akrun
akrun

Reputation: 887118

If we are applying the function on each row, use apply. Also, instead of specifying the row elements one by one as arguments (as it can differ for each dataset), use the ... which can take any number of elements a arguments, and create the matrix out of it

chisquare.tableMod <- function(...){

     t <- matrix(c(...), nrow = 2) 
      chisq.test(t)

   }

out <- apply(df1, 1, chisquare.tableMod)

Testing with the output from OP's function

chisquare.table <- function(var1, var2, var3, var4){
     t <- matrix(c(var1, var2, var3, var4), nrow = 2)


     chisq.test(t)
  }

outOld <- chisquare.table(80, 99920, 85, 99915)
identical(out[[1]], outOld)
#[1] TRUE

As @42- mentioned in the comments, apply returns a matrix and matrix can hold only single class. So, select only those columns that are numeric while working with apply (or only single class)

data

df1 <- data.frame(v1 = c(80, 79, 49), v2 = c(99920, 98230, 43240),
      v3  = c(85, 40, 35), v4 = c(99915, 43265, 43238))

Upvotes: 1

Related Questions