I want to apply a function to every row of a data frame. Using apply, the result is not itself a data frame again, it looks more like a list or matrix? (I don\'t know enough
You can use the apply family but, you're right, the result is either a matrix
or a list
. Not a big deal though to get back to a data.frame
.
Your function needs to return something consistent across columns (raw iris
instead of iris[, 1:4]
would not work below, because of iris$Species
which is a factor with 3 levels where summary
returns 6 numeric from a numeric column) and that's where a reproducible would help. Below, I used iris
and summary
:
as.data.frame(apply(iris[, 1:4], 2, summary))
as.data.frame(sapply(iris[, 1:4], summary))
do.call(cbind, lapply(iris[, 1:4], summary))