本文介绍了使用mutate将函数一次应用于多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些数据:

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"))

看起来像这样:

> 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"...

我想更改ADR列:star_rating。具体来说,我想对任何逗号进行加减。

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

我尝试过:

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

但是这会引发错误:

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

以r为基数:

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

但是,如果我可以在dplyr链中做到这一点,那将很方便,这意味着我不需要;不必写出每个变量就像声明var时一样,我可以仅使用ADR:star_rating。

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.

如何在dplyr中使用mutate实现此功能?

How can I achieve this in dplyr with mutate?

推荐答案

我认为它已经快到了。我用过 mutate_at (我认为 mutate_each 已弃用),并将变量名称包含在 vars 内:


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

这篇关于使用mutate将函数一次应用于多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:25
查看更多