0

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...

C.L.
  • 3
  • 2
  • Welcome to SO. Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), along with expected output. – Sotos Jun 23 '17 at 09:43
  • you coud try: `ifelse(data$x.Koordinate == dplyr::lag(data$x.Koordinate, 1) & data$G > 16, data$x.Koordinate, NA)` without using any function or loop just the `ifelse`. Or try only base R: `ifelse(data$x.Koordinate == c(NA, data$x.Koordinate[-length(data$x.Koordinate)]) & data$G > 16,T, F)` – Roman Jun 23 '17 at 09:49
  • You have spelling mistakes in `Kooridnate` and your `[i+1]` tries to reference beyond the last row of the data frame. – Andrew Gustar Jun 23 '17 at 09:56
  • Hi Andrew, Ow sorry, yes, I have done spelling mistakes... After changing this, I have a result, but still not what I need. Here is the code: `test<- function(x.Koordinate,data) { for (i in 1:length(data$x.Koordinate)) { result<-ifelse(data$x.Koordinate[i] == data$x.Koordinate[i+1], ifelse(data$G[i+1]<16, "null", data$x.Koordinate[i+1])) return(result) }} test(x.Koordinate,data) [1] 205450` – C.L. Jun 23 '17 at 10:12
  • I think I don't understand what you want to say with "your [i+1] tries to reference beyond the last row of the data frame" I would like that R take the first coordinate of the x.Koordinate variable, and then compare it to the 9 others. If they are different, I would like to have a return on this coordinate. I've put the `[i+1]` so that R compare the first value with the next one. How can I write it right? Thanks a lot – C.L. Jun 23 '17 at 10:12

1 Answers1

0
  • Your first ifelse doesn't have any yes or no parameter.
  • You don't need 2 ifelse just because you have 2 conditions.
  • You can test directly a == b, why test a/b == 1 instead ?
  • In your 1st example you're not assigning the result of your ifelse to anything
  • In your last example you try to return result at every iteration of the loop
  • Item i+1 won't exist if i goes up to length(data$x.Koordinate)
  • You spelled "Koordinate" as "Kooridnate" in the second time it appears in both of your tries

I'm not sure how you want the output to be stored, but I think this gives you what you want:

for (i in 1:(length(data$x.Koordinate)-1)) {
  print(ifelse(data$x.Koordinate[i] == data$x.Koordinate[i+1] & data$G[i+1]<16,"nothing",data$x.Koordinate[i+1]))
}
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • Perfect, that's what I needed! Thanks a lot for your help, and for your comments on my code! I take notes and will do better the next time – C.L. Jun 23 '17 at 10:17
  • But, still, there is something I don't understand. Maybe I don't get the process of the loop... Here the result with your proposition: `> for (i in 1:(length(data$x.Koordinate)-1)) { + print(ifelse(data$x.Koordinate[i] == data$x.Koordinate[i+1] & data$G[i+1]<16,"nothing",data$x.Koordinate[i+1])) + } [1] 205450 [1] 205500 [1] "nothing" [1] 205500 [1] 205550 [1] 205550 [1] "nothing" [1] 205600 [1] 205600` Normally, the loop should'nt bring to coordinate which are the same, but here it is the case: two times 205600, 205550, and 205500 – C.L. Jun 23 '17 at 10:27