1. Subsetting and Sorting
set.seed(13435)
X
X<-X[sample(1:5),];X$var2[c(1,3)]=NA #更改X
X
X[,1]
X[,"var1"]
X[1:2,"var2"]
Logicals ands and ors (选择)
X[(X$var1<=3&X$var3>11),]
X[(X$var1<=3|X$var3>15),]
Dealing with missing values
X[which(X$var2>8),]
Sorting
sort(X$var1)
sort(X$var1,decreasing=TRUE)
sort(X$var2,na.last=TRUE)
Ordering
X[order(X$var1),]
X[order(X$var1,X$var3),]
Ordering with plyr
library(plyr)
arrange(X,var1)
arrange(X,desc(var1))
Adding rows and columns
X$var4<-rnorm(5) #将var4加入
X
Y<-cbind(X,rnorm(5))
Y
2.Summarizing Data
Getting the data from the web
if(!file.exists("./data")){dir.create("./data")}
fileUrl<-"https://data.baltimorecity.gov/api/views/k5ry-ef3g/rows.csv?accessType=DOWNLOAD"
download.file(fileUrl,destfile="./data/restaurants.csv",method="curl")
restData<-read.csv("./data/restaurants.csv")
Look a bit at the data
head(restData,n=3) #查看前三行数据
tail(restData,n=3) #查看后三行数据
Make summary
summary(restData)
str(restData) #看更深的数据
quantile(restData$councilDistrict,na.rm=TRUE) #看分位数
quantile(restData$councilDistrict,probs=c(0.5,0.75,0.9))
Make table
table(restData$zipCode,useNA="ifany")
table(restData$councilDistrict,restData$zipCode)
Check for missing values
sum(is.na(restData$councilDistrict))
any(is.na(restData$councilDistrict))
all(restData$zipCode>0)
Row and column sums
colSums(is.na(restData))
all(colSums(is.na(restData))==0) #返回TRUE/FALSE
Values with specific characteristics
table(restData$zipCode%in%c("21212"))
table(restData$zipCode%in%c("21212","21213"))
Values with specific characteristics
restData[restData$zipCode%in%c("21212","21213"),]
Cross tabs #把数据根据变量分组查看
data(UCBAdmissions)
DF=as.data.frame(UCBAdmissions)
summary(DF)
xt<-xtabs(Freq~Gender+Admit,data=DF)
xt
Admit
Gender Admitted Rejected
Male 1198 1493
Female 557 1278
Flat tables
warpbreaks$replicate<-rep(1:9,len=54)
xt=xtabs(breaks~.,data=warpbreaks)
xt
Flat tables
Size of a data set
fakeData=rnorm(1e5)
object.size(fakeData)
print(object.size(fakeData),units="Mb")
3. Creating New Variables
Getting data from the web
if(!file.exists("./data")){dir.create("./data")}
fileUrl<-"https://data.baltimorecity.gov/api/views/k5ry-ef3g/rows.csv?accessType=DOWNLOAD"
download.file(fileUrl,destfile="./data/restaurants.csv",method="curl")
restData<-read.csv("./data/restaurants.csv")
Creating sequences
Sometimes you need an index for your data set
s1<-seq(1,10,by=2) ;s1
[1] 1 3 5 7 9
s2<-seq(1,10,length=3);s2
[1] 1.0 5.5 10.0
x<-c(1,3,8,25,100); seq(along=x)
[1] 1 2 3 4 5
Subsetting variables
restData$nearMe=restData$neighborhood%in%c("Roland Park","Homeland")
table(restData$nearMe)
FALSE TRUE
1314 13
Creating binary variables
restData$zipWrong=ifelse(restData$zipCode<0,TRUE,FALSE)
table(restData$zipWrong,restData$zipCode<0)
FALSE TRUE
FALSE 1326 0
TRUE 0 1
Creating categorical variables
restData$zipGroups=cut(restData$zipCode,breaks=quantile(restData$zipCode))
table(restData$zipGroups)
table(restData$zipGroups,restData$zipCode)
Easier cutting
library(Hmisc)
restData$zipGroups=cut2(restData$zipCode,g=4)
table(restData$zipGroups)
Creating factor variables
restData$zcf<-factor(restData$zipCode)
restData$zcf[1:10]
class(restData$zcf)
[1] "factor"
Levels of factor variables
yesno<-sample(c("yes","no"),size=10,replace=TRUE)
yesnofac=factor(yesno,levels=c("yes","no"))
relevel(yesnofac,ref="no")
[1] yes yes yes yes no yes yes yes no no
Levels: no yes
as.numeric(yesnofac)
[1] 1 1 1 1 2 1 1 1 2 2
Cutting produces factor variables
library(Hmisc)
restData$zipGroups=cut2(restData$zipCode,g=4)
table(restData$zipGroups)
[-21226,21205) [ 21205,21220) [ 21220,21227) [ 21227,21287]
338 375 300 314
Using the mutate function
library(Hmisc); library(plyr)
restData2=mutate(restData,zipGroups=cut2(zipCode,g=4))
table(restData2$zipGroups)
[-21226,21205) [ 21205,21220) [ 21220,21227) [ 21227,21287]
338 375 300 314
Common transforms
abs(x) absolute value
sqrt(x) square root
ceiling(x) ceiling(3.475) is 4
floor(x) floor(3.475) is 3
round(x,digits=n) round(3.475,digits=2) is 3.48
signif(x,digits=n) signif(3.475,digits=2) is 3.5
cos(x), sin(x) etc.
log(x) natural logarithm
log2(x),log10(x)other common logs
exp(x) exponentiating x
4. Reshaping Data
Start with reshaping
library(reshape2)
head(mtcars) #返回一组以车辆型号为obs的序列,var有各型号的马力数据