I have a dataframe with 12665x784 dimension and I need to call a function over each row (this function expects an input of 1x784 from the dataframe and a constant with the same size) and store the result in another vector. The code using for loop is shown bellow:
sample <- 1:20
my.constant <- 1:5
dim(sample) <- c(4, 5)
df <- as.data.frame(sample)
my.function <- function (x, y){
y <- as.matrix(y)
x <- as.matrix(x)
return(x%*%y)
}
result <- c()
for (row in 1:nrow(df)) {
result<- c(result, my.function(df[row, ], my.constant))
}
I've tried the following approach, but it didn't work:
result <- sapply(lapply(df, as.vector), my.function, my.constant)