考虑这个简单的例子:

library(dplyr)

dataframe <- data_frame(helloo = c(1,2,3,4,5,6),
                        ooooHH = c(1,1,1,2,2,2),
                        ahaaa = c(200,400,120,300,100,100))

# A tibble: 6 x 3
  helloo ooooHH ahaaa
   <dbl>  <dbl> <dbl>
1      1      1   200
2      2      1   400
3      3      1   120
4      4      2   300
5      5      2   100
6      6      2   100

在这里,我想将函数ntile应用于包含oo的所有列,但我希望这些新列被称为cat+对应的列.

我知道我能做到

dataframe %>% mutate_at(vars(contains('oo')), .funs = funs(ntile(., 2)))
# A tibble: 6 x 3
  helloo ooooHH ahaaa
   <int>  <int> <dbl>
1      1      1   200
2      1      1   400
3      1      1   120
4      2      2   300
5      2      2   100
6      2      2   100

但我需要的是这个

# A tibble: 8 x 5
  helloo   ooooHH   ahaaa cat_helloo cat_ooooHH
     <dbl>    <dbl> <dbl>    <int>    <int>
1        1        1   200        1        1
2        2        1   400        1        1
3        3        1   120        1        1
4        4        2   300        2        2
5        5        2   100        2        2
6        5        2   100        2        2
7        6        2   100        2        2
8        6        2   100        2        2

有没有一种解决方案不需要存储中间数据并合并回原始数据帧?

推荐答案

Update 2020-06 for dplyr 1.0.0

dplyr 1.0.0开始,across()函数取代mutate_at()等函数的"范围变量".代码在across()内看起来应该很熟悉,它嵌套在mutate()内.

向列表中给定的函数添加名称会将函数名称添加为后缀.

dataframe %>%
     mutate( across(contains('oo'), 
                    .fns = list(cat = ~ntile(., 2))) )

# A tibble: 6 x 5
  helloo ooooHH ahaaa helloo_cat ooooHH_cat
   <dbl>  <dbl> <dbl>      <int>      <int>
1      1      1   200          1          1
2      2      1   400          1          1
3      3      1   120          1          1
4      4      2   300          2          2
5      5      2   100          2          2
6      6      2   100          2          2

在1.0.0中,使用across()中的.names参数,更改新列名会更容易一些.下面是一个将函数名作为前缀而不是后缀添加的示例.它使用glue语法.

dataframe %>%
     mutate( across(contains('oo'), 
                    .fns = list(cat = ~ntile(., 2)),
                    .names = "{fn}_{col}" ) )

# A tibble: 6 x 5
  helloo ooooHH ahaaa cat_helloo cat_ooooHH
   <dbl>  <dbl> <dbl>      <int>      <int>
1      1      1   200          1          1
2      2      1   400          1          1
3      3      1   120          1          1
4      4      2   300          2          2
5      5      2   100          2          2
6      6      2   100          2          2

Original answer with mutate_at()

Edited to reflect changes in dplyr. As of dplyr 0.8.0, 100 is deprecated and 101 with 102 should be used instead.

您可以给传递给.funs的列表中的函数命名,以生成新变量,并将名称作为后缀.

dataframe %>% mutate_at(vars(contains('oo')), .funs = list(cat = ~ntile(., 2)))

# A tibble: 6 x 5
  helloo ooooHH ahaaa helloo_cat ooooHH_cat
   <dbl>  <dbl> <dbl>      <int>      <int>
1      1      1   200          1          1
2      2      1   400          1          1
3      3      1   120          1          1
4      4      2   300          2          2
5      5      2   100          2          2
6      6      2   100          2          2

如果你想把它作为前缀,你可以用rename_at来改变名字.

dataframe %>% 
     mutate_at(vars(contains('oo')), .funs = list(cat = ~ntile(., 2))) %>%
     rename_at( vars( contains( "_cat") ), list( ~paste("cat", gsub("_cat", "", .), sep = "_") ) )

# A tibble: 6 x 5
  helloo ooooHH ahaaa cat_helloo cat_ooooHH
   <dbl>  <dbl> <dbl>      <int>      <int>
1      1      1   200          1          1
2      2      1   400          1          1
3      3      1   120          1          1
4      4      2   300          2          2
5      5      2   100          2          2
6      6      2   100          2          2

dplyr早期版本中包含funs()的早期代码:

dataframe %>% 
     mutate_at(vars(contains('oo')), .funs = funs(cat = ntile(., 2))) %>%
     rename_at( vars( contains( "_cat") ), funs( paste("cat", gsub("_cat", "", .), sep = "_") ) )

R相关问答推荐

R图中的字体大小和字体样式(带有R底图)

从载体创建 pyramid

使用na.locf在长格式数据集中输入具有多个时间点的数据集

按崩溃类别分类的指数

有没有方法将琴弦完全捕捉到R中的多边形?

geom_Ribbon条件填充创建与数据不匹配的形状(ggplot 2 r)

按R中的组查找相邻列的行累积和的最大值

在另一个函数中调用ggplot2美学

如何使用列表中多个列表中的第一条记录创建数据框

即使硬币没有被抛出,也要保持对其的跟踪

使用R闪光显示所有数据点作为默认设置

对于变量的每个值,仅 Select 包含列表中所有值的值.R

将选定的索引范围与阈值进行比较

有没有一种方法可以同时对rhandsontable进行排序和从rhandsontable中删除?

如何将一些单元格的内容随机 Select 到一个数据框中?

删除数据帧中特定行号之间的每第三行和第四行

按组和连续id计算日期差

如何在条形图中的x和填充变量中包含多个响应变量?

主题(Legend.key=Element_RECT(Fill=&Quot;White&Quot;))不起作用

R-找出存在其他变量的各种大小的所有组合