Use apply
:
apply(d, 1, skewness, na.rm =TRUE)
A fixed version of your for
loop:
skew <- vector("numeric", nrow(d)) # first pre allocate to hold the result
for (i in 1:nrow(d)){
skew[i] <- skewness(d[i, ], na.rm=TRUE) # your matrix indexing was wrong
}
setNames(skew, rownames(d)) # just for names
Note that your matrix indexing was completely wrong, and as NA
s are present in your matrix, you need to set na.rm=TRUE
to skip them, read ?skewness
. I prefer using apply
instead of a for
loop, read about ?apply
.