數(shù)據(jù)的導(dǎo)入與導(dǎo)出
getwd() #得到當前文件存放的工作目錄
setwd() #重新設(shè)置當前文件存放的工作目錄
read.csv("數(shù)據(jù)文件所在工作目錄") #導(dǎo)入數(shù)據(jù)文件
write.csv(數(shù)據(jù)集, "定義一個文件名稱") #導(dǎo)出數(shù)據(jù)文件
數(shù)據(jù)初級管理
創(chuàng)建新變量
變量名 <- 表達式
表達式由操作符、函數(shù)、變量、常量等組成,操作符包括運算操作符和條件操作符。下表展示了常見的運算操作符:

下表展示了常見的邏輯運算符,邏輯運算符表達式可返 回TRUE或FALSE:

下面舉例說明如何創(chuàng)建新變量:
# 新建一個數(shù)據(jù)框,用來儲存成績
teams <- c("PHI","NYM","FLA","ATL","WSN")
> score1 <- c(90,87,78,90,67)
> score2 <- c(70,87,78,65,90)
> score_frame = data.frame(teams,score1,score2)
> score_frame
teams score1 score2
1 PHI 90 70
2 NYM 87 87
3 FLA 78 78
4 ATL 90 65
5 WSN 67 90
# 在數(shù)據(jù)框中添加新的變量score的平均值
> score_frame <- transform(score_frame,mean_score = (score1 + score2)/2)
> score_frame
teams score1 score2 mean_score
1 PHI 90 70 80.0
2 NYM 87 87 87.0
3 FLA 78 78 78.0
4 ATL 90 65 77.5
5 WSN 67 90 78.5
順便一提,transform函數(shù)的作用有為原數(shù)據(jù)框添加新的列,改變原變量列的值,還可通過賦值NULL刪除列變量,即重新“改造”數(shù)據(jù)框。例如我們要刪除上述數(shù)據(jù)框中的mean_score列:
> score_frame <- transform(score_frame,mean_score = NULL)
> score_frame
teams score1 score2
1 PHI 90 70
2 NYM 87 87
3 FLA 78 78
4 ATL 90 65
5 WSN 67 90
變量的重編碼
重編碼涉及根據(jù)同一個變量和/或其他變量的現(xiàn)有值創(chuàng)建新值的過程。例如上述例子中我們想要根據(jù)成績的高低劃分等級,平均成績大于等于80為”Excellent“,大于等于60,小于80為“Passed",這些等級構(gòu)成了新的類別型變量,區(qū)別于原來的連續(xù)型變量,代碼如下:
> score_frame <- within(score_frame,{
+ degree <- NULL
+ degree[mean_score >= 80] <- "Excellent"
+ degree[mean_score < 80 & mean_score >=60] <- "Passed"
+ })
> print(score_frame)
teams score1 score2 mean_score degree
1 PHI 90 70 80.0 Excellent
2 NYM 87 87 87.0 Excellent
3 FLA 78 78 78.0 Passed
4 ATL 90 65 77.5 Passed
5 WSN 67 90 78.5 Passed
從以上代碼可以看出在此次新建新變量時使用了within函數(shù),看起來貌似與前面使用的transform函數(shù)相似,與with函數(shù)的外形也極易混淆,因此特對比三函數(shù):
with(data, expr, ...)
within(data, expr, ...)
transform(data,...)
# data 常用的數(shù)據(jù)格式有l(wèi)ist 或 data frame。但是對于with()函數(shù),它還可以是 an environment 或 an integer as in sys.call
# expr 使用數(shù)據(jù)內(nèi)容進行評估執(zhí)行的一個或多個表達式。注意,如果有多個表達式,則需要用花括號括起來。
with()函數(shù)是一個泛型函數(shù),由數(shù)據(jù)構(gòu)建的本地環(huán)境,評估執(zhí)行R的表達式(命令)。環(huán)境將調(diào)用者的環(huán)境作為其父環(huán)境。這對于簡化調(diào)用建模函數(shù)非常有用。within()函數(shù)與with()函數(shù)類似,兩者的區(qū)別在于,within()函數(shù)在評估執(zhí)行R的表達式(命令)之后檢查環(huán)境,并對數(shù)據(jù)的副本(a copy of data)做出相應(yīng)的更改,然后再返回帶有這些更改內(nèi)容的新對象。簡單理解就是,with()函數(shù): 返回評估執(zhí)行R表達式的值,within()函數(shù): 返回修改對象
transform函數(shù)只能用于數(shù)據(jù)框改變,而within函數(shù)應(yīng)用更寬泛一些,可以用于除數(shù)據(jù)框之外的數(shù)據(jù)對象的改變。
下面的代碼幫大家理解何時用哪個函數(shù):
# 數(shù)據(jù)框的單個改動,with()的代碼相對簡潔
anorexia$wtDiff <- with(anorexia, Postwt - Prewt)
anorexia <- within(anorexia, wtDiff2 <- Postwt - Prewt)
anorexia <- transform(anorexia, wtDiff3 = Postwt - Prewt)
# 數(shù)據(jù)框的多個改動,with()的代碼相對冗長繁瑣,推薦使用 within() 和 transform()
fahrenheit_to_celcius <- function(f) (f - 32) / 1.8
airquality[c("cTemp", "logOzone", "MonthName")] <- with(airquality, list(
fahrenheit_to_celcius(Temp),
log(Ozone),
month.abb[Month]
))
airquality <- within(airquality,
{
cTemp2 <- fahrenheit_to_celcius(Temp)
logOzone2 <- log(Ozone)
MonthName2 <- month.abb[Month]
})
airquality <- transform(airquality,
cTemp3 = fahrenheit_to_celcius(Temp),
logOzone3 = log(Ozone),
MonthName3 = month.abb[Month]
)
變量的重命名
在R語言中變量是在賦值時才創(chuàng)建的,不能事先進行聲明。這里變量重命名實際上指的是給變量中的子變量重命名,如果從面向?qū)ο蟮慕嵌葋碚f,就象是修改一個類的屬性名,其數(shù)據(jù)保持不變。簡單一句話,為對象更改一個”標簽“。下面我們以數(shù)據(jù)框為例:
1. 使用可視化界面修改
使用 fix( ) 來調(diào)用一個交互式的編輯器,單擊變量名,然后在彈出的對話框中將其重命名,如下:
>fix(score_frame)
但是當我輸入時發(fā)生了報錯(本人用的是Mac OS)
> fix(score_frame)
Error in check_for_XQuartz() :
X11 library is missing: install XQuartz from xquartz.macosforge.org
大致就是缺一個xQuartz的東西,于是我去網(wǎng)址下載了xQuartz。
https://www.xquartz.org
注意在下載安裝后,要重啟電腦,否則又會報錯。

在這里即可修改變量名。
2. 使用命令行修改
需要先安裝 reshape 包,然后用 reshape 包中的 rename( ) 函數(shù)來重命名。
# 將score1改名為math,score2改名為english
> library(reshape)
> score_frame <- rename(score_frame,c(score1 = "math",score2 = "english"))
> print(score_frame)
teams math english mean_score degree
1 PHI 90 70 80.0 Excellent
2 NYM 87 87 87.0 Excellent
3 FLA 78 78 78.0 Passed
4 ATL 90 65 77.5 Passed
5 WSN 67 90 78.5 Passed
當然,我們也可以用之前就學(xué)過的 names( ) 函數(shù):
> names(score_frame)
[1] "teams" "math" "english" "mean_score" "degree"
> names(score_frame)[2] <- "Math"
> print(score_frame)
teams Math english mean_score degree
1 PHI 90 70 80.0 Excellent
2 NYM 87 87 87.0 Excellent
3 FLA 78 78 78.0 Passed
4 ATL 90 65 77.5 Passed
5 WSN 67 90 78.5 Passed