寻找关于如何使用dplyr的mutate_if语句检查我是否需要将列转换为因子变量的一些建议。
这个功能说明了我试图做的事情。问题是,当我想为" max_value"传递某些内容时,正确的语法是什么?我的函数中的参数?
不起作用 - 我尝试在功能中更改参数。
funct_change <- function(x, max_value ){
max(x, na.rm = TRUE) >max_value
}
mtcars %>% mutate_if( funct_change(max_value=30), as.character) %>% glimpse()
工作 - 我对参数进行了硬编码
funct_change <- function(x, max_value=30 ){
max(x, na.rm = TRUE) >max_value
}
mtcars %>% mutate_if( funct_change, as.character) %>% glimpse()
答案 0 :(得分:1)
如果您要向.predicate
mutate_if
以及.funs
dplyr
中出现的其他地方~
提供除功能名称以外的任何内容,您需要执行以下操作之一:
...
基本上做同样的事情。.funs
允许您向max_value = 30
添加额外的参数,因此您可以将mutate
作为参数提供给mutate_if
。对于.predicate
,仅适用于要应用的功能,而不适用于funct_change <- function(x, max_value){
max(x, na.rm = TRUE) > max_value
}
library(dplyr)
mtcars %>% mutate_if(function(x) funct_change(x, 30), as.character) %>% glimpse()
#> Observations: 32
#> Variables: 11
#> $ mpg <chr> "21", "21", "22.8", "21.4", "18.7", "18.1", "14.3", "24.4...
#> $ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, ...
#> $ disp <chr> "160", "160", "108", "258", "360", "225", "360", "146.7",...
#> $ hp <chr> "110", "110", "93", "110", "175", "105", "245", "62", "95...
#> $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.9...
#> $ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3...
#> $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 2...
#> $ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, ...
#> $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, ...
mtcars %>% mutate_if(~ funct_change(., 30), as.character) %>% glimpse()
#> Observations: 32
#> Variables: 11
#> $ mpg <chr> "21", "21", "22.8", "21.4", "18.7", "18.1", "14.3", "24.4...
#> $ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, ...
#> $ disp <chr> "160", "160", "108", "258", "360", "225", "360", "146.7",...
#> $ hp <chr> "110", "110", "93", "110", "175", "105", "245", "62", "95...
#> $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.9...
#> $ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3...
#> $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 2...
#> $ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, ...
#> $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, ...
。def example_plot(ax, somedata, somedata2, title, xlab, ylab, fontsize=12):
ax.plot(somedata, color='b')
ax.plot(somedata2, color='g')
ax.locator_params(nbins=3)
ax.set_xlabel(xlab, fontsize=8)
ax.set_ylabel(ylab, fontsize=10)
ax.set_title(title, fontsize=10)
import matplotlib.pyplot as plt
import numpy as np
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
ax1.grid(color='r', linestyle='-', linewidth=1)
ax2.grid(color='r', linestyle='-', linewidth=1)
ax3.grid(color='r', linestyle='-', linewidth=1)
ax4.grid(color='r', linestyle='-', linewidth=1)
example_plot(ax1,cumulative1,survival1, t1, xlbl1, ylbl1)
example_plot(ax2,cumulative2,survival2, t2, xlbl2, ylbl2)
example_plot(ax3,cumulative3,survival3, t3, xlbl3, ylbl3)
example_plot(ax4,cumulative4,survival4, t4, xlbl4, ylbl4)
plt.tight_layout(pad= 0.4, w_pad= 0.5, h_pad = 1.0)
由reprex package(v0.2.0)创建于2018-04-10。