# Apply function to each row in r Dataframe
# Creating dataset
# creating firs column
x <- c(5,6,7,5,6,9)
# creating second column
y <- c(6,4,3,4,2,6)
# creating third column
z <- c(1,2,3,7,8,9)
# creating dataframe
df <- data.frame(A=x,B=y,C=z)
# creating function to computer product
product = function(x,output){
# accessing elements from first column
A = x[1]
# accessing elements from second column
B=x[2]
# accessing elements from third column
C= x[3]
# return product
return(A*B*C)
}
# apply(X,MARGIN,FUN,...)
apply(df,1,product )
# apply(X,MARGIN,FUN,...)
single <- apply(df,1,product )
# adding product vector to dataframe
cbind(df,product = single)