简体   繁体   中英

How to get the largest value (range 1-2) from column 3 in a 4th column grouped_by the id's in column 1?

I want R to replace all the rows with the same chargepoint_skey and socketsinuse by just one row with that particular chargepoint_skey and socketsinuse. Ofcourse this needs to be done for all the individual chargepoint_skeys. So a kind of summarize. How can I get this done?

My code so far to get the dataframe (see image):

vraag_5 <- FACT_CHARGESSESION_JOINDIMLOCATION %>%
             select(ChargePoint_skey, Socket_ID) %>%
             group_by(ChargePoint_skey) %>%
             mutate(Socket_ID = str_replace_all(Socket_ID, "XXXXXX", "0")) %>%
             mutate(socketsinuse = max(Socket_ID))

Update after changing code:

dataframe3

Try changing your code to:

vraag_5 <- FACT_CHARGESSESION_JOINDIMLOCATION %>%
                mutate(Socket_ID = as.numeric(stringr::str_replace_all(Socket_ID,
                                   "XXXXXX", "0"))) %>%
                group_by(ChargePoint_skey) %>%
                summarise(socketsinuse = max(Socket_ID, na.rm = TRUE))

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