|
The ... argument is also necessary when the number of arguments passed to the function cannot be known in advance.
> args(paste) ## view the description of arguments of function `paste`.
function (...,collapse = NULL)
NULL
> args(cat)
function (...,fill = FALSE,labels = NULL,append = FALSE)
NULL
> paste("a",sep = ":")
[1] "a:b"
> paste("a",se = ":")
[1] "a b :"
Scoping Rules
A Diversion on Binding Values to Symbol
When R tries to bind a value to a symbol,it searches through a series of environments to find the apropriate value. When you are working on the command line and need to retrieve the value of an R object,the order is roughly
- Search the global environment for a symbol name matching the one requested.
- Search the namespaces of each of the packages on the search list.
Free Variable
> z <- 1
> lm <- function(x,y) {
+ x + y + z ## z is a free variable
+ }
> lm(1,1)
[1] 3
Coding Standard
- Always use text files / text editor.
- Indent your code.
- Limit the width of your code.
- Limit the length of your function.
Dates and Times
- Dates are represented by the Date class
- Times are represented by the
POSIXct or the POSIXlt class
- Dates are stored internally as the number of days since 1970-01-01
- Times are stored internally as the number of seconds since 1970-01-01
> Sys.time()
[1] "2016-07-13 22:22:37 CST"
> timeNow <- Sys.time()
> datestring <- c(timeNow)
> x <- strptime(datestring,"%B %d,%Y %H:%M") ## format the time string
> x
[1] NA
> class(x)
[1] "POSIXlt" "POSIXt"
Loop Functions
-
lapply Loop over a list and evaluate a functin on each element.
-
sapply Same as lapply but try to simplify the result.
-
apply Apply a function over the margins of an array.
-
taply Apply a function over subsets of a vector.
-
mapply Multivariate version of lapply.
- An auxiliary function
split is also useful,particularly in conjunction with lapply.
lapply
lapply returns a list of the same length as X,each element of which is the result of applying FUN to the corresponding element of X.
> lapply
function (X,FUN,...)
{
FUN <- match.fun(FUN)
if (!is.vector(X) || is.object(X))
X <- as.list(X)
.Internal(lapply(X,FUN))
}
<bytecode: 0x000000000b606e90>
<environment: namespace:base>
For an instance below.
> x <- list(a = 1:5,b = rnorm(10))
> lapply(x,mean)
$a
[1] 3
$b
[1] -0.1931699
-
rnorm: Density,distribution function,quantile function and random generation for the normal distribution with mean equal to mean and standard deviation equal to sd.
-
runif,dunif,punif,qunif: These functions provide information about the uniform distribution on the interval from min to max. dunif gives the density,punif gives the distribution function qunif gives the quantile function and runif generates random deviates.
> x <- list(a = matrix(1:4,2),b = matrix(1:6,3,2))
> lapply(x,function(elt) elt[,1])
$a
[1] 1 2
$b
[1] 1 2 3
sapply
sapply will try to simplify the result of lapply if possible.
- If the result is a list where every element is length 1,then a vector is returned.
- If the result is a list where every element is a vector of the same length (>1),a matrix is returned.
- If it can't figure things out,a list is returned.
apply
apply is used to a evaluate a function (often an anonymous one) over the margins of an array.
- It is most often used to apply a function to the rows or columns of a matrix.
- It can be used with general arrays,e.g. taking the average of an array of matrices.
- It is not really faster than writing a loop,but it works in one line!
> str(apply)
function (X,MARGIN,...)
-
x is an array
-
MARGIN is an integer vector indicating which margins should be "retained"
-
FUN is a function to be applied.
-
... is for other arguments to be passed to FUN
> x <- matrix(1:4,2)
> x
[,2]
[1,] 1 3
[2,] 2 4
> apply(x,mean)
[1] 2 3
> apply(x,mean)
[1] 1.5 3.5
-
MARGIN = 1 Compute the mean at every row,and return a vector as result.
-
MARGIN = 1 Compute the mean at every column,and return a vector as result.
Other shortcuts.
- rowSums = apply(x,sum)
- rowMeans = apply(x,mean)
- colSums = apply(x,sum)
- colMeans = apply(x,mean)
Apply in multiple dimensions array,in the source below,we use a vector as a MARGIN value to complete the compute of multiple dimensions compute.
> a <- array(rnorm(2 * 2 * 10),c(2,10))
> apply(a,c(1,mean)
[,1] [,] 0.6869065 -0.66529430
[2,] -0.1136978 -0.04124547
mapply
mapply is a multivariate apply of sorts which applies a function in parallel over a set of arguments.
> str(mapply)
function (FUN,...,MoreArgs = NULL,SIMPLIFY = TRUE,USE.NAMES = TRUE)
-
FUN is a function to apply.
-
... contains arguments to apply over.
-
MoreArgs is a list of other arguments to FUN.
-
SIMPLIFY indicates whether the result should be simplified.
tapply
tapply is used to apply a function over subsets of a vector.
split
split divides the data in the vector x into the groups defined by f. The replacement forms replace values corresponding to such a division. unsplit reverses the effect of split.
> s <- split(airquality,airquality$Month)
> sapply(s,function(x) colMeans(x[,c("Ozone","Wind")]))
5 6 7 8 9
Ozone NA NA NA NA NA
Wind 11.62258 10.26667 8.941935 8.793548 10.18
(编辑:安卓应用网_ASP源码网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|