标签:
manager<-c(1,2,3,4,5) data<-c("10/24/08","10/28/08","10/1/08","10/12/08","5/1/09") country<-c("US","US","UK","UK","UK") gender<-c("M","F","F","M","F") age<-c(32,45,25,39,99) q1<-c(5,3,3,3,2) q2<-c(4,5,5,3,2) q3<-c(5,2,5,4,1) q4<-c(5,5,5,NA,2) q5<-c(5,5,2,NA,1) leadership<-data.frame(manager,data,country,gender,age,q1,q2,q3,q4,q5,stringASFactors=FALSE)
将leadship数据集中经理人的年龄变量age重编码为类别型变量
leadership within <-(leadership,{ agecat <-NA agecat[age>75]<-"Elder" agecat[age>=55&age<=75]<-"Middle Age" agecat[age<55]<-"Young" })
fix(leadship)#交互的方式修改变量的名称
reshape内的rename函数
ibrary(reshape) leadership<-rename(leadership, c(manage="mamanageID",data="testdata") )#通过reshape库将名字更改
name()函数来重命名
names(leadship)[2]<"testdata"
> y<-c(1,2,3,NA)
> is.na(y)
[1] FALSE FALSE FALSE TRUE #检测y内是否有空值存在
x<-(1,2,3,NA) y<-sum(x,na.rm = TRUE)#na.rm计算之前排除缺失值,并使用剩余值进行计算
删除不完整的数据
newdata<-na.omit(leadership)#使用na.omit 函数
newdata<-leadership[order(leadership$age),]#按照年龄排序
attach(leadership) newdata<-leadership[order(gender,-age),] detach(leadership)#性别 年龄降序排列
attach(leadership) newdata<-leadership[which(gender==‘M‘&age>30),] detach(leadership) newdata #挑选性别为男且年龄大于30岁的人
mysample<-leadership[sample(1:nrow(leadership),3,replace=FALSE),]
4.15 SQL
标签:
原文地址:http://www.cnblogs.com/py-Tom/p/4803835.html