繁体   English   中英

为嵌套for循环编写,其中语句在R中具有多个条件

[英]Writing for nested for loop where statements have multiple conditions in R

我试图根据篮球场的某些区域确定射门的投篮命中率,包括禁区,油漆区(不包括禁区),所有其他2个指针,3号角和其他所有3个区域。

我所拥有的是结果(制造或错过)或每次拍摄的x,y坐标。 我尝试做的是编写一个for循环来接收行和行信息,并使用嵌套的ifelse语句检查是否满足多个条件。

for (i in cs){shotarea[i] <- ifelse(x[i] >= -125 & x[i] <= 125 & y[i] < x[i]*tan(acos(x[i]/125)),1,
                  ifelse(x[i] => -125 & x[i] <= 125 & y[i] < 460 & y[i] > 460-x[i]*tan(acos(x[i]/125)),2,
                  ifelse(x[i] >= -245 & x[i] < -125 & y[i] < 460,2,
                  ifelse(x[i] <= 245 & x[i] > 125 & y[i] < 460,2,
                  ifelse(x[i] >= -660 & x[i] < -245 & y[i] < x[i]*tan(acos(x[i]/675)),3,
                  ifelse(x[i] > -245 & x[i] < 245 & y[i] < x[i]*tan(acos(x[i]/675)) & y > 460,3,
                  ifelse(x[i] > 245 & x[i] <= 660 & y[i] < x[i]*tan(acos(x[i]/675)),3,
                  ifelse(x[i] > -750 & x[i] < -660 & y[i] < x[i]*tan(acos(660/675)),4,
                  ifelse(x[i] < 750 & x[i] > 660 & y[i] < x[i]*tan(acos(660/675)),4,
                  ifelse(x[i] > -750 & x[i] < -660 & y[i] > x[i]*tan(acos(660/675)),5,
                  ifelse(x[i] < 750 & x[i] > 660 & y[i] > x[i]*tan(acos(660/675)),5,
                  ifelse(x[i] > -660 & x[i] < 660 & y[i] > x[i]*tan(acos(x[i]/675)),5,6))))))))))))}

我用几何函数限制了球场上的所有区域:
限制区域= x(-125,125),y(inf,x tan acos(x / 125))

油漆,不包括限制区域=(x(-245,-125)U(125,245),y(inf,460))和(x(-125,125),y(x tan acos(x / 125),460))

所有其他2 =(x(-660,-245)U(245,660),y(inf,x [i] tan acos(x [i] / 675))和(x(-245,245),y(460,x [i] tan acos(x [i] / 675))

角3 = x(-750,-660)U(660,750),y(inf,x [i] tan acos(660/675)

所有其他3 = =(x(-750,-660)U)(660,750),y(x [i] tan acos(660/675),inf))和(x(-660,660),y(x [i] tan acos(x [i] / 675),inf)

x值的范围为(-750,750),y值来自(-87,972),其中(0,0)是篮子的中心,x值范围从边线到边线。 我有几个数据子集,我根据x值的范围运行。 我运行一个子集,其中x范围(-125,125),一个运行它(-675,-125)U(125,675),另一个运行它(-750,-675)U(675,750)。 (我这样做是为了防止arccos()表达式作为预防措施返回NAN,但我认为我并不需要)。 您还会注意到我使用了i的范围向量,因此我可以将我制作的不同子集分出来。 不确定这是不是问题。

每当我尝试运行它,我就会得到

Error: unexpected ')' in:"ifelse((x[i] < 750 && x[i] > 660 && y[i] > x[i]*tan*acos(660/675)),5,
                  ifelse((x[i] > -660 && x[i] < 660 && y[i] > x[i]*tan*acos(x[i]/675)),5,6)))))))))))", > }Error: unexpected '}' in "}"

我仔细检查并三重检查,以确保我没有投入冗余的括号或括号,但我找不到一个。 所以我想我要求的是两件事之一:

是否存在可能正在创建此错误的代码的其他问题?

AND / OR

你会推荐更好的东西吗? 我敢肯定必须有一些“应用”方式才能做到这一点,但我在申请时非常不确定并且不知道从哪里开始。

EDIT: For sample data, you could use x <- runif(3580,min=-749,max=749); y <- runif(3580,min=-86,max=971)

EDIT: I went through and made sure the tan() functions had parenthesis, but otherwise removed what I hope are redundant parethenses

您可以使用dplyrcase_when函数更简单地完成此操作。 这种方法完成了两件事。 首先,它消除了for循环,从而减少了评估时间。 其次,它不使用所有ifelse语句。

library(dplyr)
x <- runif(3580,min=-749,max=749); y <- runif(3580,min=-86,max=971)
df <- data.frame(x = x, y = y)
df1 <- df %>%
  mutate(shotarea = case_when(.$x >= -125 & .$x <= 125 & .$y < .$x*tan(acos(.$x/125)) ~ "restricted",
                              .$x >= -125 & .$x <= 125 & .$y < 460 & .$y > 460-.$x*tan(acos(.$x/125)) ~ "paint",
                              ((.$x >= -245 & .$x < -125) | (.$x > 125 & .$x <= 245)) & .$y < 460 ~ "paint",
                              .$x > -245 & .$x < 245 & .$y < .$x*tan(acos(.$x/675)) & .$y > 460 ~ "other 2",
                              ((.$x >= -660 & .$x < -245) | (.$x > 245 & .$x <= 660)) & .$y < .$x*tan(acos(.$x/675)) ~ "other 2",
                              ((.$x >= -750 & .$x < -660) | (.$x > 660 & .$x <= 750)) & .$y < .$x*tan(acos(660/675)) ~ "corner 3",
                              ((.$x >= -750 & .$x < -660) | (.$x > 660 & .$x <= 750)) & .$y > .$x*tan(acos(660/675)) ~ "other 3",
                              .$x > -660 & .$x < 660 & .$y > .$x*tan(acos(.$x/675)) ~ "other 3",
                              TRUE ~ "other"))

那么你的输出将是:

> head(df1)
           x         y shotarea
1 -719.26311 733.67983  other 3
2  294.60845 574.06028  other 2
3  423.09931  62.15332  other 2
4  597.60403 960.28765  other 3
5 -592.23907 486.24250  other 3
6   33.58853 411.12968    paint

现在,我使用实际名称而不是1-6编号系统,因为我认为这使得数据可视化和解释对于其他人以及未来的您来说更容易。 但您可以通过此设置轻松将其更改回1-6系统(其中6为“其他”)

好的,所以我发现了我的问题,而且非常愚蠢。

在第二个ifelse()语句中,有条件x [i] => -125,其中操作符的语法被切换,应该是x [i]> = -125。 领导者搞砸了如何读取所有内容,这使得R认为以下括号和括号是意外的。 翻转两个背部修复了问题。

虽然,我感谢任何其他回应,给我一些看起来更干净的想法。 我知道一个包含大量&条件语句的大型嵌套ifelse()语句看起来并不漂亮,而且编写起来很烦人。 我肯定会使用tbradley的dplyr解决方案。

暂无
暂无

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

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