简体   繁体   English

R:循环,ifelse和两个条件

[英]R: loop for, ifelse and two conditions

I try to do a loop with the function ifelse. 我尝试使用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. 关于该问题的解释:我试图对R说到我的变量x.Koordinate的每个x坐标,然后将其除以最后一个。 If the result is 1 = it's the same coordinate, if it is not, it is different coordinates. 如果结果为1 =相同的坐标,否则返回不同的坐标。 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). 因此,我希望R在相同的坐标下并且该坐标的G值小于16(第二种情况)时说“无”或“空”。 If it's different than 1, and that its value of G is bigger than 16, so give me this coordinate. 如果它不等于1,并且G的值大于16,请给我这个坐标。

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... 但这什么也没给我...

  • Your first ifelse doesn't have any yes or no parameter. 您的第一个ifelse没有任何yes或no参数。
  • You don't need 2 ifelse just because you have 2 conditions. 您不需要2 ifelse只是因为您有2个条件。
  • You can test directly a == b, why test a/b == 1 instead ? 您可以直接测试a == b,为什么要测试a / b == 1呢?
  • In your 1st example you're not assigning the result of your ifelse to anything 在第一个示例中,您没有将ifelse的结果分配给任何东西
  • 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) 如果我达到长度(data $ x.Koordinate),则项目i + 1将不存在。
  • You spelled "Koordinate" as "Kooridnate" in the second time it appears in both of your tries 您在两次尝试中都将“ Koordinate”拼写为“ Kooridnate”

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]))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM