R Sprache

来源:互联网 发布:淘宝网企业店铺标志 编辑:程序博客网 时间:2024/05/17 22:01

先看一下R能干什么

df <- mtcars?mtcarsnames(df)head(df)nrow(df)summary(df)hist(df$hp)plot(df$hp,df$qsec)cor(df$hp,df$qsec)cor(df$cyl,df$qsec)df$hpPerCyl <- df$hp/df$cyldf[order(df$hpPerCyl),]head(df[order(df$hpPerCyl),])

基础内容

如何给变量赋值

#eitherx <- 1#orx = 1

注意不要忘了空白号,x<-1会被翻译成x <- 1而不是x < -1

R的基本原子类型(atomic types)

x <- TRUE   #class(x) is "logical"x <- 1L     #class(x) is "integer"x <- 1.5    #class(x) is "numeric"x <- 1.5 + o.4i #class(x) is "complex"x <- "Text" #class(x) is "character"

R中不含有标量(scalars),标量在这里被表示为长度为1的向量。
TRUE和FALSE可以简单的用T,F来代替
Strings即可以用双括号也可以用单括号阔起来

如何调用函数

ls      #is a function objectls()    #is the object returned by calling the function

如何寻求帮助

#open help:?ls#search help for the specific topic:??correlation#other functions to analyze thingsstr(x)summary(x)class(x)attributes(x)

R的控制流

#if:if(STATEMENT)    STATEMENTelse    STATEMENT#for loop:for(name in vector)    STATEMENT#repeatrepeat    STATEMENT #until 'break' is called

有趣的地方是:if结构可以被当成statement使用(只有if结构可以):

y <- if(x > 0) "y" else "no"

另外STATEMENT也可以被用{}包围起来的一组statement代替,这些语句可以全部扔在一行,也可以分成多行写

if({x <- mean(1:5); y <- mean(1:4); x<y}){cat("evaluating the 'true' block \n")"y"}else {cat("evaluating the 'false' block \n")"n"}

向量(Vectors)

如何生成向量

x <- c(1,2,3)   #c for "c"Kombinex <- seq(1,3)   #to create a sequence, with possibility tox <- seq(1,3,by=0.5)    #-define step sizex <- seq(1,3,len=10)    #-define numer of stepsx <- 1:3        #quick notation for integer sequencesx <- rep(0,10)  #repeat first "vector" 10 timesx <- numeric(10)#"numeric" vector of length 10x[1] = 10x[2] = 20x[3] = 30

无论如何c()的结果都会被铺平所以试试这个吧:

x <- c(1,c(c(2,3),c(4,5,6)))

另外原子向量中是允许缺值的:

c(T,F,T,NA)c(1:5,NA)c("A",NA,"B","C",NA)

如果在一个Vector中混合不同类型的值会怎么样呢

因为vector中的值的类性应该一直保持一致,因此当上述情况发生时,R会强制改变类型。他们的优先顺序是:logical,integer,numeric,complex,character

#notice how the type changes when you remove elements from the endx <- c(T,1L,1.0,1i,"text")#notice that modifying elements can change the whole typex <- rep(F,10)x[6] = "text"

关于vector的一些重要函数

x <- runif(10)length(x)sum(x)#statisticsmean(x)median(x)var(x)sd(x)quantile(x)#range relatedmin(x)max(x)range(x)#sorting relatedsort(x)rank(x)order(x)#vectorized functionsx + 1x * 10x < 0.5(x < 0.2)|(x > 0.8)sin(x)exp(x)round(x)

如果用不同长度的两个vector进行运算会怎么样

c(1,2,3,4) + c(1,2)c(1,2,3,4) + c(1,2,3)c(1,2,3,4) > 1

R采用的是循环的机制,但这也就要求长的vector的长度应该是短的vector的长度的整数倍

如何使用索引

x <- 1:10 * 10#a positive index select an element#a negative index omits an elementx[3]x[-3]#indices can again be vectorsx[c(1,3,5)]x[3:5]x[-c(1,3,5)]x[-(3:5)]#note:mixing positive and negative indices does not make sense#indices can also be logical vectorsx[c(T,F,T,F,T,F,F,F,F,F)]x[x == 10 | x == 30]#indices can even be namednames(x) <- c("a","b","c","d","e","f","g","h","i","j")xx["c"]

矩阵和数组

R的vector其实就是其他语言中的array。他没有纬度的概念,只有长度。
R的array是特指多维的array。他有确定的纬度。但是本质上,他也是由vector实现的
R的matrix就是指一个二维的array

如何构建一个matrix

#generate without a vectorm <- matrix(nrow=2, ncol=3)m[1,1] = 1m[2,1] = 2m[1,2] = 3m[2,2] = 4m[1,3] = 5m[2,3] = 6#generate from ONE vectormatrix(1:6, nrow=2, ncol=3)matrix(1:6, nrow=2)matrix(1:6, ncol=3)matrix(1:6, byrow=TRUE, ncol=3)#generate from multiple column/row vectorsrbind(c(1,3,5),c(2,4,6))cbind(1:2,3:4,5:6)

因为matrix本质上就是一个拥有纬度的vector,因此也可以通过vector来构造matrix

m <- 1:12dim(m) <- c(2,6)dim(m) <- c(3,2,2)c(is.vector(m),is.matrix(m),is.array(m))

matrix又是如何使用索引的呢

和vector一样

m[2,2]m[1,1:3]m[1,]m[,2]

唯一不同的是现在要给元素起名的话,要加上纬度属性了

colnames(m) <- c("A","B","C")rownames(m) <- c("o1","o2")dimnames(m)attibutes(m)

进行简单的线性运算

m <- cbind(1:2,3:4,5:6)#multiply by vectorsm %*% c(1,2,4)c(5,6) %*% m

第一个c三行一列,第二个c一行两列。真心不知道他在玩什么。以为他能自动转化吗,于是试了以下用c(1,2,3,4) %% m.结果却是出现错误,我还以为可以自动翻译成两行两列呢??然后又试了以下,5 %% m。结果还是错了

#multiply by matricesm %*% matrix(runif(6),nrow=3)#typical unary operatorsm <- cbind(1:2,3:4)t(m)        #transposediag(m)     #diagonalsolve(m)    #inverseeigen(m)    #eigenvector/eigenvalues#solving linear equationssolve(m,c(1,1)) #solve mx=c(1,1) for x#misc matrix functionsdim(m)nrow(m)ncol(m)rowSums(m)colSums(m)rowMeans(m)colMeans(m)

其他数据类型

R语言中的list

#to construct a listl <- list(name="Joe", unemployed=FALSE, salary=50000)#naming the fields is optional l <- list("Joe",FALSE,50000)#in fact, the mechanism to set the "names" is the same as for vectorsnames(l) <- c("name", "unemployed", "salary")#access single elementsl[[1]]l$namel["name]l$sal #it is even allowed to abbreviate the names if it is unique#generate a "sub" listl[c(1,3)]l[c("name","salary")]l[1]#"fields" can be added dynamicallyl$department <- "IT"l[["position"]] <- c("consultant","developer")#note that this can create gaps and unnamed fieldsl[[8]] <- TRUE

好像只有在$后面的才能以省略

R语言中的factor

f <- factor(c("A","B","C","A","B","A","A"), ordered = T)attributes(f)levels(f)summary(f)#to rename a categorylevels(f)[1] <- "a"#to convert to a numeric typeas.numeric(f)#or:attributes(f) <- NULL

R语言的data.frame

#create a data frame manuallydf <- data.frame(x=runif(20), y=runif(20), type=as.factor(runif(20) > 0.5))str(df)#create a new "feature"df$z <- df$x * df$ydf#sort by a featurepermutation <- order(df$x)df <- df[permutation,]#remove featurestoRemove <- c("x","y")toRemoveLogical <- names(df) %in% toRemovedf <- df[,!toRemoveLogical]df <- df[,!toRemoveLogical, drop= FALSE]#better if only one feature is kept#there is also the powerful "subset" function#parameter "subset" works on rows#parameter "subset" works on columnssubset(df, subset = x > 0.5, select = c(y,z))

什么是S3/S4 classes

不知??

Functions

如何定义一个function

nameOfMyFunction <- function(a,b){return (a+b)}

注意,return需要有圆括号,另外如果没有return的话,那就默认返回最后一个statement

如何定义函数的默认参数

myfunc <- function(range = 1 : myelin) range^2myfunc(1:5)mylen=10myfunc()mylen=5myfunc()rm(malen)myfunc()

如何返回多于一个值

myfunc <- function(){    list(result=42,additional="no errors",numberOfIterations=4)}ret <- myfunc()ret$resultret$numberOfIterations

补充

R里是否有预定义的变量

答案是有得如:
mtcars
Nile
iris
diamonds

如何连接两个strings

注意不能直接用“+”连接起来

paste("concatenate", "these", "strings")paste("concatenate", "these", "strings", seq="")

也可以用paste0(…),他的效率比paste好一点点

如何标准输出

x <- 1:10cat(sprintf("the sum of %d elements is %f\n", length(x), sum(x)))

如何生成随机数

hist(rnorm(1000))hist(runif(1000))hist(rbeta(1000,2,4))hist(rbinom(1000,3,0.5))

Plotting

#histograms:hist(rnom(1000))#scatter plots:plot(rnom(100), rnorm(100)#line plots:x <- seq(0, 2*pi, len=100)plot(x, sin(x)^2+tan(x), type='l'

Numerical Measures

duration = faithful$eruptionsmean(duration)median(duration)quantile(duration)quantile(duration,c(.37,.45,.99))var(duration)   #variancewaiting = faithful$waitingcov(duration,waiting)   #covariancecor(duration, waiting)  #correlation coefficientcov(duration, waiting)/(sd(duration)*sd(waiting))#standard deviations

概率分布

//待补

静态测试

//待补

0 0