個人查漏補缺裂垦,以下全部內(nèi)容均翻譯自 An Introduction to R ,原文鏈接:
https://cran.r-project.org/doc/manuals/r-release/R-intro.html#The-R-environment
2.1 Vectors and assignment
Assignment can also be made using the functionassign(). Anequivalent way of making the same assignment as above is with:(將值依次賦給x)
> assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))等同于
> c(10.4, 5.6, 3.1, 6.4, 21.7) -> x
> x[1]
> 1/x #得到5個結(jié)果
> y <- c(x, 0, x)
> y #得到11個結(jié)果 [1] 10.4? 5.6? 3.1? 6.4 21.7? 0.0 10.4? 5.6? 3.1? 6.4 21.7
通過以上的方式特碳,可以批量處理一些繁瑣的數(shù)據(jù),無需單獨寫循環(huán)語句.
2.3 Generating regular sequences 生成規(guī)則數(shù)據(jù)集
舉個例子
> seq(-5, 5, by=.2) -> s3 生成從-5開始测萎,5結(jié)束届巩,步長為0.5的數(shù)據(jù)集,等同于:
> s4 <- seq(length=51, from=-5, by=.2)
rep()函數(shù)恕汇, 生成重復(fù)數(shù)據(jù)集.
> s5 <- rep(x, times=5)
> s6 <- rep(x, each=5)
2.4 Logical vectors邏輯型向量
> x
[1] 10.4? 5.6? 3.1? 6.4 21.7
> temp <- x > 13
> temp
[1] FALSE FALSE FALSE FALSE? TRUE
2.5 Missing values缺失值
> z <- c(1:3,NA); ind <- is.na(z)#返回邏輯向量
> z
[1]? 1? 2? 3 NA
> ind
[1] FALSE FALSE FALSE? TRUE
2.6 Character vectors 特征型向量
> labs <- paste(c("X","Y"), 1:10, sep="")
添加特征型向量: c("X1", "Y2", "X3", "Y4", "X5", "Y6", "X7", "Y8", "X9", "Y10")
3.4 The class of an object
"numeric","logical","character"or"list",
but"matrix","array","factor"and"data.frame"are other possible values.
假定class是一個數(shù)據(jù)框或辖,unclass函數(shù)將會打印成一個傳統(tǒng)的列表.
> unclass(winter)
4 Ordered and unordered factors 因子排序
舉個例子
state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa", "qld", "vic", "nsw", "vic", "qld", "qld", "sa",? "tas", "sa",? "nt",? "wa",? "vic", "qld", "nsw", "nsw", "wa", "sa",? "act", "nsw", "vic", "vic", "act")
statef <- factor(state)
> statef
[1] tas sa? qld nsw nsw nt? wa? wa? qld vic nsw vic qld qld sa
[16] tas sa? nt? wa? vic qld nsw nsw wa? sa? act nsw vic vic act
Levels:? act nsw nt qld sa tas vic wa
> levels(statef)
[1] "act" "nsw" "nt"? "qld" "sa"? "tas" "vic" "wa"
4.2 The functiontapply()and ragged arrays
繼續(xù)之前的例子,重新賦予一些值給statef颂暇,
> incomes <- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,
? ? ? ? ? ? ? 61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46,
? ? ? ? ? ? ? 59, 46, 58, 43)
使用函數(shù) tapply() 計算均值:
> incmeans <- tapply(incomes, statef, mean)
act? ? nsw? ? nt? ? qld? ? sa? ? tas? ? vic? ? wa
44.500 57.333 55.500 53.600 55.000 60.500 56.000 52.250
> stdError = function(x) sqrt(var(x)/length(x))
> incmeans <- tapply(incomes, statef, stdError)
6.3.2attach() and detach()
attach() 函數(shù)處理列表list or 數(shù)據(jù)框dataframe.假定lentils 這個數(shù)據(jù)框有三個變量 lentils$u,lentils$v,lentils$w.
> attach(lentils) #函數(shù)執(zhí)行類似下面的操作:
> u <- v+w
> lentils$u <- v+w
To detach a data frame, use the function
> detach()
6.3.5 Managing the search path
> search()
[1] ".GlobalEnv"? "Autoloads"? ? "package:base"
ls(2)