I'd like to use a variable in my function but I can't figure out how to do this. Here is my function and the call on a data.frame:
errorByAleles <- function(values){
counts1 <- as.data.frame(table(values), stringsAsFactors = FALSE)
modal_value1 <- which.max(counts1$Freq)
div <- nrow(values)
return ((sum(counts1$Freq)-counts1$Freq[modal_value1])/div)
}
error1 <- apply(X=ind1[,2:9],MARGIN=2,FUN=errorByAleles)
Dimmension of data.frame on which I apply my function :
> dim(ind1)
[1] 9 9
The problem is with div <- nrow(values). div = 9 is what I need here. So how to get nrow for my "values" inside function ? Am I clear ?
Any help would be greatly appreciated !
divis only defined within the scope of your function. You'll need to either change the function or calculate the value ofdivseparately outside of the function.MARGIN=2requires an array of at least two dimensions, butind1[2:9]is a vector, maybe you meant to doind1[2:9, ]orind1[, 2:9]?ind1hasdim9x9, why doesind1[2:9]not throw an error when you callapply? That shouldn't work, are you missing a comma somewhere, likeind1[2:9,]orind1[,2:9]?