apply a function to several columns at once with mutate

后端 未结 1 567
名媛妹妹
名媛妹妹 2020-12-11 10:27

Some data:

x <- structure(list(X. = c(\"4,084\", \"4,084\", \"4,084\", \"4,084\", \"4,084\"
), ADR = c(\"1,099.69\", \"68.66\", \"232.72\", \"195.66\", \"         


        
相关标签:
1条回答
  • 2020-12-11 10:52

    I think it was nearly there. I've used mutate_at (I think mutate_each is deprecated) and included the variable names inside vars:

    library(dplyr)
    x %>% mutate_at(vars(ADR:star_rating), funs(stringr::str_replace_all(., ",", "")))
    #>      X.     ADR hotel_id city_id star_rating accommodation_type_name
    #> 1 4,084 1099.69  2313076    9395           5                   Hotel
    #> 2 4,084   68.66   583666   17193           2                Bungalow
    #> 3 4,084  232.72  1251372    5085           3                   Hotel
    #> 4 4,084  195.66  1545890   16808           4                   Hotel
    #> 5 4,084      98   298160    8584           4                   Hotel
    #>   chain_hotel booking_date checkin_date checkout_date city
    #> 1       chain    10/5/2016   10/27/2016    10/30/2016    A
    #> 2   non-chain    12/4/2016    12/9/2016    12/12/2016    B
    #> 3   non-chain    11/6/2016   11/18/2016    11/20/2016    C
    #> 4   non-chain   10/22/2016    11/3/2016     11/4/2016    D
    #> 5   non-chain   12/11/2016   12/11/2016    12/12/2016    E
    
    0 讨论(0)
提交回复
热议问题