1. Subsetting and Sorting
set.seed(13435)
X<data.frame("var1"=sample(1:5),"var2"=sample(6:10),"var3"=sample(11:15))
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) ?#查看前三行數(shù)據(jù)
tail(restData,n=3)? #查看后三行數(shù)據(jù)
Make summary
summary(restData)
str(restData) ?L缚觯看更深的數(shù)據(jù)
quantile(restData$councilDistrict,na.rm=TRUE) =9矗看分位數(shù)
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 ?#把數(shù)據(jù)根據(jù)變量分組查看?
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")
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
restData$nearMe=restData$neighborhood%in%c("Roland Park","Homeland")
table(restData$nearMe)
? ? FALSE? TRUE
? ? 1314? ? 13
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)
library(Hmisc)
restData$zipGroups=cut2(restData$zipCode,g=4)
table(restData$zipGroups)
restData$zcf<-factor(restData$zipCode)
restData$zcf[1:10]
class(restData$zcf)
[1] "factor"
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
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
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有各型號的馬力數(shù)據(jù)