I'm using an algorithm that looks like this:
df <- read.table(sep=" ", header=T, text="
x y z label
1 3 2 a
2 4 4 b
3 8 5 c
4 5 6 a
5 1 8 f")
f <- function(refObs, label) {
d <- vector()
for (i in which(df$label==label))
d <- c(d, dist(rbind (refObs, df[i,1:dim]) , method ="euclidean") )
return(dist)
}
Problem is that when I call this function with dim=1 I sometimes get "$ operator is invalid for atomic vectors". Is there a way to call columns from dataframes that won't fail me? To be more specific: can I call the f function assigning empty labels?