[R] Ops.factor(point1, point2) : - not meaningful for factors
Rui Barradas
ruipbarradas at sapo.pt
Thu Oct 4 12:56:23 CEST 2012
Hello,
First of all, it's not a good idea to use 'dist' and 'diff' since they
already are names of R functions.
As for the problem, your column DSi$Si is not a column of 2-dim points,
it's not even a vector of numbers, it's a categorical variable, a
factor, like you can see in the output of str(). See the difference with
an example, using your first two "points".
mydist <- function (point1, point2){
dff <- point1 - point2
distance <- sqrt(sum(dff^2))
distance
}
x <- c(5, 20) # these are vectors representing
y <- c(20, 2) # 2-dim points
dist(matrix(c(x, y), ncol=2))
mydist(x, y)
Also, it's better to post data using ?dput().
Hope this helps,
Rui Barradas
Em 04-10-2012 09:07, nadiah escreveu:
> Hi, can anyone help me in this problem :(. I am a total beginner in R
> software. It took me 2 days just to look into this problem. Due to this
> problem. I cant do looping. i want to find the distance between x and DSi
>
>
>> DSi
> i Si
> 1 1 (5, 20)
> 2 2 (20, 2)
> 3 3 (25, 32)
> 4 4 (8, 39)
> 5 5 (10, 17)
> 6 6 (35, 20)
> 7 7 (38, 10)
>> str (DSi)
> 'data.frame': 7 obs. of 2 variables:
> $ i : int 1 2 3 4 5 6 7
> $ Si: Factor w/ 7 levels "(10, 17)","(20, 2)",..: 6 2 3 7 1 4 5
>> x <- c(15, 45)
>> y <- DSi [,2]
>> dist <- function (point1, point2)
> + {
> + diff <- point1-point2
> + distance <- sqrt(sum(diff^2))
> + distance
> + }
>> dist (x, y)
> [1] NA
> Warning message:
> In Ops.factor(point1, point2) : - not meaningful for factors
> Regards,
> Nadiah
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Ops-factor-point1-point2-not-meaningful-for-factors-tp4644971.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list