【学习经验】--关于R的函数学习

2019-04-10 16:44:29 浏览数 (1)

太忙了,没空学习,晚上在群里看有比我还新的新人在问关于函数用法的问题,正好分享一下自己在学习中的一点小经验,其实是很简单的了:

1.help

遇到不会用的函数第一反应肯定是help的。比如不懂max怎么用:

运行

> ?max

就会有:

Maxima and Minima Description Returns the (parallel) maxima and minima of the input values. Usage max(..., na.rm = FALSE) min(..., na.rm = FALSE) pmax(..., na.rm = FALSE) pmin(..., na.rm = FALSE) pmax.int(..., na.rm = FALSE) pmin.int(..., na.rm = FALSE)

描述和用法说的都很清楚了

如果还不是很清楚,就可以运行实例

2.example

> example(max) max> require(stats); require(graphics) max> min(5:1, pi) #-> one number [1] 1 max> pmin(5:1, pi) #-> 5 numbers [1] 3.141593 3.141593 3.000000 2.000000 1.000000 max> x <- sort(rnorm(100)); cH <- 1.35 max> pmin(cH, quantile(x)) # no names [1] -1.7831769 -0.4434655 0.3276721 0.8547441 1.3500000 max> pmin(quantile(x), cH) # has names 0% 25% 50% 75% 100% -1.7831769 -0.4434655 0.3276721 0.8547441 1.3500000 max> plot(x, pmin(cH, pmax(-cH, x)), type = "b", main = "Huber's function") Hit <Return> to see next plot: max> cut01 <- function(x) pmax(pmin(x, 1), 0) max> curve( x^2 - 1/4, -1.4, 1.5, col = 2)

Hit <Return> to see next plot: max> curve(cut01(x^2 - 1/4), col = "blue", add = TRUE, n = 500) max> ## pmax(), pmin() preserve attributes of *first* argument max> D <- diag(x = (3:1)/4) ; n0 <- numeric() max> stopifnot(identical(D, cut01(D) ), max identical(n0, cut01(n0)), max identical(n0, cut01(NULL)), max identical(n0, pmax(3:1, n0, 2)), max identical(n0, pmax(n0, 4)))

最好的学习方法就是问题解决。

结束。

0 人点赞