简体   繁体   中英

How to use sapply - switch logic

I have data frame that I am using for a small educational project.

                            EVTYPE PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP 
192527 URBAN/SMALL STREAM FLOODING     0.0          5       0            
192938                  HEAVY SNOW     1.7          5       0            
193995                        HAIL    30.0          5      25          M 
194223          THUNDERSTORM WINDS     0.1          5       0            
195672          THUNDERSTORM WINDS     0.0          5       0            
198497          THUNDERSTORM WINDS    10.0          5       0            

My objective is to create a new column named PropAmtDmg and takes the following form. If PROPDMGEXP = "5" then 5 * PROPDMG

t1$PropAmtDmg <- ifelse(t1$PROPDMGEXP == "7", t1$PROPDMG * 7,
                        ifelse(t1$PROPDMGEXP == "5", t1$PROPDMG * 5,
                               0))

I might of more cases than just two that I mentioned. I would like to do this in sapply .

I would like to suggest the use of data.table for this task. data.table is a package that enhances data frames inherent in R. It is very fast . The benefit of this is there is not constant recopying of data so that if your data is large, this is memory efficient. Let's assume that your data frame is called dfr :

require(data.table)
set.seed(123) #set the seed so this can be replicated
dtb = data.table(PROPDMGEXP = sample(1:10, 10), PROPDMG = sample(1:10,10)) #sample data.table
dtb[(PROPDMGEXP %in% c(5,7)),rslt:=PROPDMG*PROPDMGEXP]

You are done. Here is the result:

    PROPDMGEXP PROPDMG rslt
 1:          3      10   NA
 2:          8       5   NA
 3:          4       6   NA
 4:          7       9   63
 5:          6       1   NA
 6:          1       7   NA
 7:         10       8   NA
 8:          9       4   NA
 9:          2       3   NA
10:          5       2   10

Note: if you want to make all the other entries 0 you can do this instead:

dtb[,rslt:=0][(PROPDMGEXP %in% c(5,7)),rslt:=PROPDMG*PROPDMGEXP]

您可以将所有条件汇总成一个唯一的条件,如下所示:

transform(t1,PropAmtDmg=ifelse(PROPDMGEXP %in% c(5,7),PROPDMG*PROPDMGEXP,0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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