数字开关语句

时间:2015-11-26 13:19:29

标签: r

我有以下数据框:

head(df_final)
MATCHID COMPETITION            TEAM1              TEAM2 GOALS1 GOALS2 RESULT  EXPG1 EXPG2     DATUM     TIJD VERSCHIL  CLAS type           TYPE
1 1696873  Pro League   Standard Liège Sporting Charleroi      3      0    TEAM1  1.57  0.61 25-7-2014 18:30:00     0.96  0.96  TBD (-0.0767,1.54]
2 1696883  Pro League Waasland-Beveren        Club Brugge      0      2  TEAM2  1.29  1.18 26-7-2014 16:00:00     0.11  0.11  TBD (-0.0767,1.54]
3 1696879  Pro League           Lierse        KV Oostende      2      0  TEAM1  1.03  1.04 26-7-2014 18:00:00    -0.01 -0.01  TBD (-0.0767,1.54]
4 1696881  Pro League         Westerlo            Lokeren      1      0  TEAM1  1.76  1.24 26-7-2014 18:00:00     0.52  0.52  TBD (-0.0767,1.54]
5 1696885  Pro League    Cercle Brugge               Gent      0      0 GELIJK  1.24  1.27 26-7-2014 18:00:00    -0.03 -0.03  TBD (-0.0767,1.54]
6 1696877  Pro League         Mechelen               Genk      3      1  TEAM1  1.60  1.23 27-7-2014 12:30:00     0.37  0.37  TBD (-0.0767,1.54]

我想添加一个名为RESULT_STRENGTH的列,根据列VERSCHIL对匹配进行分类。

如果VERSCHIL大于0.3 RESULT_STRENGTH应为HIGH,如果VERSCHIL < -0.3RESULT_STRENGTH应为MEDIUMVERSCHIL > -0.3 & <0.3然后RESULT_STRENGTH应为LOW;

因此我写了以下函数:

classes <- function(df) {

 df$VERSCHIL <- as.numeric(df$VERSCHIL)
 AA <- df$VERSCHIL

 #create new column with default
 df$STRENGTH_TYPE <- "DEFAULT"

 switch(AA, 
     ">0.3" ={
       df$STRENGTH_TYPE <- "HIGH"
     },
     "<-0.3"={
       df$STRENGTH_TYPE <- "LOW"    
     },
     ">-0.3 & <0.3" ={
       df$STRENGTH_TYPE <-  "MEDIUM"
     }
 )
}

但是当我运行classes(df_final)时,我收到以下错误:

 Error in switch(AA, `>0.3` = { : EXPR must be a length 1 vector

对这里出了什么问题的想法?

1 个答案:

答案 0 :(得分:1)

因为switch只是不这样做(更仔细地阅读?switch)......?

尝试类似

的内容
AA <- c(-0.5,0,0.5)
cut(AA,breaks=c(-Inf,-0.3,0.3,Inf),
       labels=c("LOW","MEDIUM","HIGH"))
## [1] LOW    MEDIUM HIGH  
## Levels: LOW MEDIUM HIGH

(如果你真的不想要一个因素,请使用as.character()。)