加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

『Data Science』R语言学习笔记,基础语法

发布时间:2021-02-28 05:03:27 所属栏目:大数据 来源:网络整理
导读:Data Types Data Object Vector x - c(0.5,0.6) ## numericx - c(TRUE,FALSE) ## logicalx - c(T,F) ## logicalx - c("a","b","c") ## characterx - 9:29 ## integerx - c(1+0i,2+4i) ## complexx - vector("numeric",length = 10) ## create a numeric vect

... is often used when extending another function and you don't want to copy the entire argument list of the original function.

myplot <- function(x,y,type = "1",...) {
  plot(x,type = type,...)
}

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

  1. Search the global environment for a symbol name matching the one requested.
  2. 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

  1. Always use text files / text editor.
  2. Indent your code.
  3. Limit the width of your code.
  4. 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

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读