數(shù)據(jù)集的導(dǎo)入
數(shù)據(jù)分析時(shí)大部分文件都是以特定分隔符分隔的,處理這類文件都可以使用read.table()
函數(shù),將文件讀入為數(shù)據(jù)框凛篙。read.table()
常用參數(shù)如下:
參數(shù) | 描述 |
---|---|
header | 是否將文件首行作為數(shù)據(jù)框列名 |
row.names | 用于指定行名稱 |
col.names | 用于指定列名稱,使用此參數(shù)時(shí)需指定header=FALSE软吐,若header和col.names都未設(shè)置,則會(huì)自動(dòng)分配列名c(v1,v2,...) |
sep | 指定分隔符筒主,默認(rèn)為sep=""关噪,表示多個(gè)空格鸟蟹、tab乌妙、換行或回車 |
na.strings | 用于表示缺失值得字符向量,如na.strings=c('-9','?')表示字符-9和建钥?在讀入時(shí)會(huì)自動(dòng)轉(zhuǎn)換成NA |
colClasses | 為每一列分配數(shù)據(jù)類型藤韵,如colClasses=c('numeric', 'character', 'bool', 'NULL') |
quote | 用于對有特殊字符的字符串劃定界限的字符串。默認(rèn)為"或'熊经。 |
skip | 讀入數(shù)據(jù)時(shí)跳過的行泽艘,數(shù)據(jù)有頭注釋時(shí)比較有用 |
stringASFactors | 邏輯變量,設(shè)置字符型數(shù)據(jù)是否轉(zhuǎn)換成因子镐依,默認(rèn)為TRUE匹涮,一般設(shè)置為FALSE。設(shè)置為FALSE時(shí)處理大文件時(shí)可以提高處理速度 |
text | 指定要進(jìn)行處理的數(shù)據(jù)槐壳,若設(shè)置了text則file則應(yīng)留空 |
數(shù)據(jù)的基本處理
1然低、變量的創(chuàng)建以及重命名
變量創(chuàng)建直接使用<-
即可,數(shù)據(jù)框的重命名可使用names()
方法或colname()
、rowname()
雳攘。
數(shù)據(jù)框中合并或者添加行列可使用cbind()
带兜、rbind()
或者功能更為強(qiáng)大的merge()
方法。使用transform()
可使代碼更加簡潔吨灭。
> student<-c('john','jack','david','rose'); english<-c(90,82,88,67);math<-c(45,48,33,25);df<-data.frame(student,english,math);df
student english math
1 john 90 45
2 jack 82 48
3 david 88 33
4 rose 67 25
> df$art=c(18,16,19,7);df<-transform(df,age=rep(c(15,16),2));df
student english math art age
1 john 90 45 18 15
2 jack 82 48 16 16
3 david 88 33 19 15
4 rose 67 25 7 16
plyr包中有一個(gè)rename()
函數(shù)可用于修改變量名刚照。
merge(x, y, by = intersect(names(x), names(y)),by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all, sort = TRUE, suffixes = c(".x",".y"), incomparables = NULL, ...)
x,y 用于合并的兩個(gè)數(shù)據(jù)框;
by,by.x,by.y 指定依據(jù)哪些行合并數(shù)據(jù)框,默認(rèn)值為相同列名的列;
all,all.x,all.y 指定x和y的行是否應(yīng)該全在輸出文件;
sort by指定的列是否要排序;
suffixes 指定除by外相同列名的后綴;
incomparables 指定by中哪些單元不進(jìn)行合并.
2、缺失值處理
在任何數(shù)據(jù)中都可能由于各種原因出現(xiàn)缺失值(使用NA表示喧兄,Not Available)无畔,一般在處理數(shù)據(jù)之前需要對缺失值進(jìn)行處理。處理缺失值時(shí)需要注意兩點(diǎn):(1)缺失值是不可比較的吠冤,只能使用處理缺失值的函數(shù)進(jìn)行識(shí)別檩互,如is.na()
;(2)無限值(Inf)或者不能出現(xiàn)的值(NaN)不是以NA進(jìn)行表示咨演,識(shí)別這些值需要用到另外的方法闸昨,如is.infinite()
和is.nan()
。此外薄风,含有缺失值得計(jì)算結(jié)果也為缺失值饵较。
大部分函數(shù)都會(huì)有一個(gè)na.rm=TRUE
的參數(shù),可在計(jì)算前去除缺失值遭赂。
簡單的去除缺失值的行可使用na.omit()
函數(shù)循诉。
> df$salary <- c(100,200,NA,400);df
student english math art age salary
1 john 90 45 18 15 100
2 jack 82 48 16 16 200
3 david 88 33 19 15 NA
4 rose 67 25 7 16 400
> is.na(df)
student english math art age salary
[1,] FALSE FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE FALSE
[3,] FALSE FALSE FALSE FALSE FALSE TRUE
[4,] FALSE FALSE FALSE FALSE FALSE FALSE
> bonus<-c(50,50,50,50);df$total<-bonus+df$salary;df
student english math art age salary total
1 john 90 45 18 15 100 150
2 jack 82 48 16 16 200 250
3 david 88 33 19 15 NA NA
4 rose 67 25 7 16 400 450
> sum(df$salary)
[1] NA
> na.omit(df)
student english math art age salary total
1 john 90 45 18 15 100 150
2 jack 82 48 16 16 200 250
4 rose 67 25 7 16 400 450
3、變量的重編碼
重編碼涉及根據(jù)同一個(gè)變量和/或其他變量創(chuàng)建新值得過程撇他。例如將連續(xù)型變量修改為類別型變量茄猫,NA填補(bǔ)為平均值,基于 一組分?jǐn)?shù)線創(chuàng)建一個(gè)表示及格或者不及格的變量等困肩。
> df$salary<-c(100,200,NA,400);df
student english math salary
1 john 90 45 100
2 jack 82 48 200
3 david 88 33 NA
4 rose 67 25 400
> df$math[df$math>40]<-'A';df$math[30<=df$math& df$math<=40]<-'B';df$math[df$math<300]<-'C';df
student english math salary
1 john 90 A 100
2 jack 82 A 200
3 david 88 B NA
4 rose 67 C 400
#一般不會(huì)直接在原變量上面進(jìn)行更改划纽,可在增加一個(gè)變量,這里為了方便直接在math在進(jìn)行了更改
> within(df,salary[is.na(salary)]<-mean(salary,na.rm=TRUE))
student english math salary
1 john 90 A 100.0000
2 jack 82 A 200.0000
3 david 88 B 233.3333
4 rose 67 C 400.0000
#使用within可使代碼更加簡潔锌畸,但是它沒有改變原來的數(shù)據(jù)
> df
student english math salary
1 john 90 A 100
2 jack 82 A 200
3 david 88 B NA
4 rose 67 C 400
> df$salary[is.na(df$salary)]<-mean(df$salary,na.rm=TRUE);df
student english math salary
1 john 90 A 100.0000
2 jack 82 A 200.0000
3 david 88 B 233.3333
4 rose 67 C 400.0000
4勇劣、日期型數(shù)據(jù)
日期常異字符串的形式輸入到R中,可使用as.Date(x, input_format)
將字符串轉(zhuǎn)化為日期變量潭枣。使用format(x,format="output_format")
可指定日期的輸出格式比默。
R中的日期格式如下表:
符號 | 含義 | 示例 |
---|---|---|
%d | 數(shù)字表示的日期(0-31) | 01-31 |
%a | 縮寫的星期名 | Mon |
%A | 非縮寫的星期名 | Monday |
%m | 月份(00-12) | 00-12 |
%b | 縮寫的月份 | Jan |
%B | 非縮寫的月份 | January |
%y | 兩位數(shù)的年份 | o8 |
%Y | 四位數(shù)的年份 | 2008 |
> mydates <- as.Date(c('2007-06-22','2007-06-23'),);mydates
[1] "2007-06-22" "2007-06-23"
> d<-as.Date(c('01/12/2008','02/10/2009'),'%m/%d/%Y');d
[1] "2008-01-12" "2009-02-10"
5、類型轉(zhuǎn)換
R中類型轉(zhuǎn)換函數(shù)如下表:
判斷 | 轉(zhuǎn)換 |
---|---|
is.numeric() | as.numeric() |
is.character() | as.character() |
is.vector() | as.vector() |
is.matrix() | as.matrix() |
is.data.frame() | as.data.frame |
is.factor() | as.factor() |
is.logical() | as.logical() |
> a<-c(1:5)
> is.numeric(a)
[1] TRUE
> as.character(a);a;is.character(a)
[1] "1" "2" "3" "4" "5"
[1] 1 2 3 4 5
[1] FALSE
6盆犁、數(shù)據(jù)排序
可使用order()
對一個(gè)數(shù)據(jù)框進(jìn)行排序命咐,默認(rèn)為升序。
> df
student english math salary
1 john 90 A 100.0000
2 jack 82 A 200.0000
3 david 88 B 233.3333
4 rose 67 C 400.0000
# "-"表示降序排序
> df[order(-df$english,df$student),]
student english math salary
1 john 90 A 100.0000
3 david 88 B 233.3333
2 jack 82 A 200.0000
4 rose 67 C 400.0000
7谐岁、數(shù)據(jù)選取子集
數(shù)據(jù)框的數(shù)據(jù)的選取既可以使用行列名進(jìn)行選取也可以通過索引進(jìn)行選取醋奠,還可以通過bool值進(jìn)行選任拖隆(如前面的例子中對NA值進(jìn)行重編碼),但是更推薦使用行列名進(jìn)行選取钝域。刪除某一列可使用"-"符號讽坏,但是這種方式只能對索引使用,對列名無法使用例证,且并未對原始數(shù)據(jù)進(jìn)行更改路呜,因此需要重新賦值。
> df;df[c("salary","student")];df[c(4,1)];df[c(TRUE,FALSE,TRUE,FALSE)]
student english math salary
1 john 90 A 100.0000
2 jack 82 A 200.0000
3 david 88 B 233.3333
4 rose 67 C 400.0000
salary student
1 100.0000 john
2 200.0000 jack
3 233.3333 david
4 400.0000 rose
salary student
1 100.0000 john
2 200.0000 jack
3 233.3333 david
4 400.0000 rose
student math
1 john A
2 jack A
3 david B
4 rose C
> df[c(1,4),]
student english math salary
1 john 90 A 100
4 rose 67 C 400
> df[-4]
student english math
1 john 90 A
2 jack 82 A
3 david 88 B
4 rose 67 C
> df[df$english>80,]
student english math salary
1 john 90 A 100.0000
2 jack 82 A 200.0000
3 david 88 B 233.3333
使用subset()
可以更簡單的選擇所需要的數(shù)據(jù)织咧。
# 選擇英語成績高于80分且數(shù)學(xué)為A的行胀葱,保留的列為student和salary列
> subset(df,math>80 & math=="A", select=c(student,salary))
student salary
1 john 100
2 jack 200
sample()
函數(shù)用于(有放回或無放回抽樣)。
#replace=FALSE表示無放回抽樣
> df
student english math salary
1 john 90 A 100.0000
2 jack 82 A 200.0000
3 david 88 B 233.3333
4 rose 67 C 400.0000
#無放回抽取三列
> df[sample(1:nrow(df), 3, replace=FALSE)]
english salary math
1 90 100.0000 A
2 82 200.0000 A
3 88 233.3333 B
4 67 400.0000 C
#無放回抽取三行
> df[sample(1:nrow(df), 3, replace=FALSE),]
student english math salary
3 david 88 B 233.3333
2 jack 82 A 200.0000
4 rose 67 C 400.0000
參考:
R語言實(shí)戰(zhàn)