代码4-1 1 > manager<-c(1,2,3,4,5)
2 > date<-c("10/24/08","10/28/08","10/1/08","10/12/08","5/1/09")
3 > country<-c("us","us","uk","uk","uk")
4 > gender<-c("m","f","f","m","f")
5 > age<-c(32,45,25,39,99)
6 > q1<-c(5,3,3,3,2)
7 > q2<-c(4,5,5,3,2)
8 > q3<-c(5,2,5,4,1)
9 > q4<-c(5,5,5,NA,2)
10 > q5<-c(5,5,2,NA,1)
11 > leadership<-data.frame(manager,date,country,gender,age,
12 + q1,q2,q3,q4,q5,stringsAsFactors = FALSE)
13 > leadership
14 manager date country gender age q1 q2 q3 q4 q5
151110/24/08 us m 3254555162210/28/08 us f 4535255173310/1/08 uk f 2535552184410/12/08 uk m 39334 NA NA
19555/1/09 uk f 9922121
语句variable[condition] <- expression将仅在condition的值为TRUE时执行赋值 1 > leadership$agecat[leadership$age<55]<-"Young" 2 > leadership$age[leadership$age=="99"] <- NA
3 > leadership$agecat[leadership$age>75]<-"Elder" 4 > leadership$agecat[leadership$age>=55 &
5 + leadership$age<=75]<-"Middle Aged" 6 > leadership$agecat[leadership$age<55]<-"Young" 7 > leadership
8 manager date country gender age q1 q2 q3 q4 q5 agecat
91110/24/08 us m 3254555 Young
102210/28/08 us f 4535255 Young
113310/1/08 uk f 2535552 Young
124410/12/08 uk m 39334 NA NA Young
13555/1/09 uk f NA 22121 <NA> 或
1 > leadership<-within(leadership,{
2 + agecat<-NA #每句后面不能有逗号
3 + agecat[age>75] <-"Elder" 4 + agecat[age>=55 & age <=75]<-"Middle Aged" 5 + agecat[age<55] <-"Young"})
6 > leadership
7 manager date country gender age q1 q2 q3 q4 q5 agecat
81110/24/08 us m 3254555 Young
92210/28/08 us f 4535255 Young
103310/1/08 uk f 2535552 Young
114410/12/08 uk m 39334 NA NA Young
12555/1/09 uk f 9922121 Elder
法一: 1 > leaderhip
2 manager date country gender age q1 q2 q3 q4 q5
31510/24/08 us m 3254555 42410/28/08 us f 4535255 53310/1/08 uk f 2535552 64210/12/08 uk m 39334 NA NA
7515/1/09 uk f 9922121 8 > fix(leaderhip) # 注意括号是英文括号,不是中文括号
9 > leaderhip
10 managerID date country gender age q1 q2 q3 q4 q5
111110/24/08 us m 3254555122210/28/08 us f 4535255133310/1/08 uk f 2535552144410/12/08 uk m 39334 NA NA
15555/1/09 uk f 9922121
法二: 1 > leaderhip<-rename(leaderhip,
2 + c(managerID="manager",date="testDate"))
3 > leaderhip
4 manager testDate country gender age q1 q2 q3 q4 q5
51110/24/08 us m 3254555 62210/28/08 us f 4535255 73310/1/08 uk f 2535552 84410/12/08 uk m 39334 NA NA
9555/1/09 uk f 9922121
法三: 1 > names(leaderhip)
2 [1] "manager""testDate""country""gender""age""q1" 3 [7] "q2""q3""q4""q5" 4 > names(leaderhip)[1]<-"managerID" 5 > leaderhip
6 managerID testDate country gender age q1 q2 q3 q4 q5
71110/24/08 us m 3254555 82210/28/08 us f 4535255 93310/1/08 uk f 2535552104410/12/08 uk m 39334 NA NA
11555/1/09 uk f 9922121
例子: 1 >leadership
2 > manager date country gender age q1 q2 q3 q4 q5
3110/24/08 us m 3254555 4210/28/08 us f 4535255 5310/1/08 uk f 2535552 6410/12/08 uk m 39334 NA NA
755/1/09 uk f 9922121 8 >leadership$date
9 [1] "10/24/08""10/28/08""10/1/08""10/12/08""5/1/09"10 >myformat<-"%m/%d/%y"11 >leadership$date<-as.Date(leadership$date,myformat)# 为什么这个格式不用加斜杠也行,而上面那个要加 斜杠
12 >leadership
13 manager date country gender age q1 q2 q3 q4 q5
1412008-10-24 us m 32545551522008-10-28 us f 45352551632008-10-01 uk f 25355521742008-10-12 uk m 39334 NA NA
1852009-05-01 uk f 9922121
请发表评论