Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

r - apply a function to several columns at once with mutate

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", "98"), hotel_id = c("2,313,076", 
"583,666", "1,251,372", "1,545,890", "298,160"), city_id = c("9,395", 
"17,193", "5,085", "16,808", "8,584"), star_rating = c(5, 2, 
3, 4, 4), accommodation_type_name = c("Hotel", "Bungalow", "Hotel", 
"Hotel", "Hotel"), chain_hotel = c("chain", "non-chain", "non-chain", 
"non-chain", "non-chain"), booking_date = c("10/5/2016", "12/4/2016", 
"11/6/2016", "10/22/2016", "12/11/2016"), checkin_date = c("10/27/2016", 
"12/9/2016", "11/18/2016", "11/3/2016", "12/11/2016"), checkout_date = c("10/30/2016", 
"12/12/2016", "11/20/2016", "11/4/2016", "12/12/2016"), city = c("A", 
"B", "C", "D", "E")), class = "data.frame", row.names = c(NA, 
-5L), .Names = c("X.", "ADR", "hotel_id", "city_id", "star_rating", 
"accommodation_type_name", "chain_hotel", "booking_date", "checkin_date", 
"checkout_date", "city"))

Looks like this:

> glimpse(x)
Observations: 27,298
Variables: 11
$ X.                      <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"...
$ ADR                     <chr> "71.06", "76.56", "153.88", "126.6", "115.08", "81.6", "77.16", "168.36",...
$ hotel_id                <chr> "297,388", "298,322", "2,313,076", "2,240,838", "2,240,838", "331,350", "...
$ city_id                 <chr> "9,395", "9,395", "9,395", "9,395", "9,395", "9,395", "9,395", "9,395", "...
$ star_rating             <dbl> 2.5, 3.0, 5.0, 3.5, 3.5, 3.0, 3.0, 5.0, 2.0, 3.0, 4.0, 2.0, 3.0, 2.0, 3.0...
$ accommadation_type_name <chr> "Hotel", "Hotel", "Hotel", "Hotel", "Hotel", "Hotel", "Hotel", "Hotel", "...
$ chain_hotel             <chr> "non-chain", "non-chain", "chain", "non-chain", "non-chain", "non-chain",...
$ booking_date            <chr> "8/2/2016", "8/2/2016", "8/2/2016", "8/4/2016", "8/4/2016", "8/4/2016", "...
$ checkin_date            <chr> "10/1/2016", "10/1/2016", "10/1/2016", "10/2/2016", "10/2/2016", "10/3/20...
$ checkout_date           <chr> "10/2/2016", "10/2/2016", "10/2/2016", "10/3/2016", "10/3/2016", "10/5/20...
$ city                    <chr> "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"...

I wouldlike to mutate columns ADR: star_rating. Specifically I want to gsub out any commas.

I tried:

x <- x %>% 
  mutate_each(ADR:star_rating, funs(gsub ",", ""))

But this throws an error:

Error: unexpected string constant in:
"x <- x %>% 
  mutate_each(ADR:star_rating, funs(gsub ",""

In base r I could d this:

vars <- c("ADR", "hotel_id", "city_id", "star_rating")
x[vars] <- lapply(x[vars], function(i) gsub(",", "", i))

However, if I could do this within a dplyr chain it would be convenient and mean I don;t have to write out each variable like I do when declaring vars, I could just use ADR:star_rating.

How can I achieve this in dplyr with mutate?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...