I try to do a loop with the function ifelse. Unfortunately it doesn't work. Here is my code:
> data$x.Koordinate<-as.numeric(data$x.Koordinate)
> data$G<-as.numeric(data$G)
> for (i in 1:length(data$x.Koordinate)) {
+ ifelse((data$x.Koordinate[i]/data$x.Kooridnate[i+1]==1)& ifelse((data$G[i+1]<16),"nothing",data$x.Koordinate[i+1]))
+ }
Error in (data$x.Koordinate[i]/data$x.Kooridnate[i + 1] == 1) & ifelse((data$G[i + :
operations are possible only for numeric, logical or complex types
Explanations on the problem: I try to say to R to go to every x coordinate of my variable x.Koordinate, and to divide it with the last one. If the result is 1 = it's the same coordinate, if it is not, it is different coordinates. So I would like that R say "nothing" or "null" when it is the same coordinate AND the value of G of this coordinate is below 16 (second condition). If it's different than 1, and that its value of G is bigger than 16, so give me this coordinate.
I do not understand why there is an error in my code, because I've already put my variables in numeric.
Here is my data:
ID Bezeichnung x.Koordinate y.Koordinate G N hdom V Mittelstamm Fi Ta Foe Lae ueN Bu Es Ei Ah ueL Struktur
1 10,809 62 205450 603950 8 1067 21 64 10 NA NA NA NA NA 100 NA NA NA NA NA
2 10,810 63 205450 604000 16 1333 22 128 12 NA NA NA NA NA 75 NA NA 25 NA NA
3 10,811 56 205500 604050 20 800 22 160 18 NA NA NA NA NA 60 NA NA NA 40 NA
4 10,812 55 205500 604000 12 1033 20 97 12 33 NA NA NA NA 67 NA NA NA NA NA
5 10,813 54 205500 603950 20 500 56 0 23 NA NA NA NA NA 100 NA NA NA NA NA
6 10,814 46 205550 604050 16 567 32 215 19 75 NA NA NA NA 25 NA NA NA NA NA
7 10,815 47 205550 604100 16 233 26 174 30 NA 25 NA NA NA 50 NA NA NA 25 NA
Thanks for the help!
EDIT
Hi, now I've tried this:
test<- function(x) { for (i in 1:length(data$x.Koordinate)) {
result<-ifelse(data$x.Koordinate[i] == data$x.Kooridnate[i+1], ifelse(data$G[i+1]<16, "null", data$x.Kooridnate[i+1]))
return(result)
}}
But it returns me nothing...