用序号字符串替换数字的最简单方法是使用ifelse函数。R中的ifelse函数的作用类似于ifelse(test_expression,x,y)。在这里,test_expression必须是逻辑向量或可以强制为逻辑的对象。返回值是一个与test_expression具有相同长度的向量。
> x1<-sample(0:1,100,replace=TRUE) > x1
输出结果
[1] 1 1 1 0 1 0 0 1 1 1 0 0 1 0 1 0 0 1 1 0 0 1 1 1 1 0 0 0 0 1 1 1 0 1 1 0 1 [38] 0 1 1 0 0 1 0 1 1 1 1 0 1 0 1 0 0 1 0 1 0 0 1 1 1 0 0 0 1 0 1 1 1 1 1 0 1 [75] 1 1 0 0 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 1 0 1 1
> x1<-ifelse(x1==0,"No","Yes") > x1
输出结果
[1] "Yes" "Yes" "Yes" "No" "Yes" "No" "No" "Yes" "Yes" "Yes" "No" "No" [13] "Yes" "No" "Yes" "No" "No" "Yes" "Yes" "No" "No" "Yes" "Yes" "Yes" [25] "Yes" "No" "No" "No" "No" "Yes" "Yes" "Yes" "No" "Yes" "Yes" "No" [37] "Yes" "No" "Yes" "Yes" "No" "No" "Yes" "No" "Yes" "Yes" "Yes" "Yes" [49] "No" "Yes" "No" "Yes" "No" "No" "Yes" "No" "Yes" "No" "No" "Yes" [61] "Yes" "Yes" "No" "No" "No" "Yes" "No" "Yes" "Yes" "Yes" "Yes" "Yes" [73] "No" "Yes" "Yes" "Yes" "No" "No" "No" "Yes" "Yes" "No" "No" "No" [85] "No" "Yes" "No" "Yes" "No" "Yes" "No" "No" "No" "No" "No" "Yes" [97] "Yes" "No" "Yes" "Yes"
> x2<-sample(-1:1,100,replace=TRUE) > x2
输出结果
[1] 1 0 0 0 -1 1 0 1 -1 -1 -1 0 1 -1 0 0 0 -1 1 0 0 1 1 1 0 [26] 0 -1 -1 1 0 1 1 -1 1 -1 1 0 0 -1 1 1 1 -1 1 0 -1 -1 0 -1 -1 [51] 1 1 0 1 1 1 0 -1 -1 0 1 0 1 -1 -1 -1 -1 1 1 1 -1 -1 -1 -1 1 [76] 1 0 -1 0 1 -1 -1 -1 0 -1 1 1 -1 1 0 -1 1 1 -1 -1 -1 0 1 0 0
> x2<-ifelse(x2==-1,"Low",ifelse(x2==0,"Medium","High")) > x2
输出结果
[1] "High" "Medium" "Medium" "Medium" "Low" "High" "Medium" "High" [9] "Low" "Low" "Low" "Medium" "High" "Low" "Medium" "Medium" [17] "Medium" "Low" "High" "Medium" "Medium" "High" "High" "High" [25] "Medium" "Medium" "Low" "Low" "High" "Medium" "High" "High" [33] "Low" "High" "Low" "High" "Medium" "Medium" "Low" "High" [41] "High" "High" "Low" "High" "Medium" "Low" "Low" "Medium" [49] "Low" "Low" "High" "High" "Medium" "High" "High" "High" [57] "Medium" "Low" "Low" "Medium" "High" "Medium" "High" "Low" [65] "Low" "Low" "Low" "High" "High" "High" "Low" "Low" [73] "Low" "Low" "High" "High" "Medium" "Low" "Medium" "High" [81] "Low" "Low" "Low" "Medium" "Low" "High" "High" "Low" [89] "High" "Medium" "Low" "High" "High" "Low" "Low" "Low" [97] "Medium" "High" "Medium" "Medium"
> x3<-sample(1:3,100,replace=TRUE) > x3
输出结果
[1] 2 1 3 2 3 1 2 1 2 2 3 1 3 2 3 3 3 3 1 3 3 2 1 1 1 3 1 2 1 3 1 2 2 1 2 1 3 [38] 3 1 3 3 2 2 3 1 3 3 2 1 1 2 3 2 2 3 3 3 2 2 2 3 3 2 3 1 3 2 1 1 3 1 1 1 2 [75] 3 1 2 3 2 3 2 1 1 1 2 3 3 2 2 2 2 2 1 3 3 2 2 2 3 1
> x3<-ifelse(x3==1,"First",ifelse(x3==2,"Second","Third")) > x3
输出结果
[1] "Second" "First" "Third" "Second" "Third" "First" "Second" "First" [9] "Second" "Second" "Third" "First" "Third" "Second" "Third" "Third" [17] "Third" "Third" "First" "Third" "Third" "Second" "First" "First" [25] "First" "Third" "First" "Second" "First" "Third" "First" "Second" [33] "Second" "First" "Second" "First" "Third" "Third" "First" "Third" [41] "Third" "Second" "Second" "Third" "First" "Third" "Third" "Second" [49] "First" "First" "Second" "Third" "Second" "Second" "Third" "Third" [57] "Third" "Second" "Second" "Second" "Third" "Third" "Second" "Third" [65] "First" "Third" "Second" "First" "First" "Third" "First" "First" [73] "First" "Second" "Third" "First" "Second" "Third" "Second" "Third" [81] "Second" "First" "First" "First" "Second" "Third" "Third" "Second" [89] "Second" "Second" "Second" "Second" "First" "Third" "Third" "Second" [97] "Second" "Second" "Third" "First"
> x4<-sample(1:5,80,replace=TRUE) > x4
输出结果
[1] 4 5 2 4 5 2 1 2 3 2 3 2 2 2 2 4 4 5 1 5 3 4 1 4 1 5 2 1 4 1 5 1 4 4 2 1 5 3 [39] 4 4 2 5 1 3 4 2 2 2 4 1 5 5 5 2 1 1 3 4 1 4 5 3 4 4 1 4 2 4 4 3 3 1 5 5 2 2 [77] 1 4 2 3
> x4<-ifelse(x4==1,"Very Satisfied",ifelse(x4==2,"Satisfied",ifelse(x4==3,"Neutral",ifelse(x4==3,"Dissatisfied","Very Dissatisfied")))) > x4
输出结果
[1] "Very Dissatisfied" "Very Dissatisfied" "Satisfied" [4] "Very Dissatisfied" "Very Dissatisfied" "Satisfied" [7] "Very Satisfied" "Satisfied" "Neutral" [10] "Satisfied" "Neutral" "Satisfied" [13] "Satisfied" "Satisfied" "Satisfied" [16] "Very Dissatisfied" "Very Dissatisfied" "Very Dissatisfied" [19] "Very Satisfied" "Very Dissatisfied" "Neutral" [22] "Very Dissatisfied" "Very Satisfied" "Very Dissatisfied" [25] "Very Satisfied" "Very Dissatisfied" "Satisfied" [28] "Very Satisfied" "Very Dissatisfied" "Very Satisfied" [31] "Very Dissatisfied" "Very Satisfied" "Very Dissatisfied" [34] "Very Dissatisfied" "Satisfied" "Very Satisfied" [37] "Very Dissatisfied" "Neutral" "Very Dissatisfied" [40] "Very Dissatisfied" "Satisfied" "Very Dissatisfied" [43] "Very Satisfied" "Neutral" "Very Dissatisfied" [46] "Satisfied" "Satisfied" "Satisfied" [49] "Very Dissatisfied" "Very Satisfied" "Very Dissatisfied" [52] "Very Dissatisfied" "Very Dissatisfied" "Satisfied" [55] "Very Satisfied" "Very Satisfied" "Neutral" [58] "Very Dissatisfied" "Very Satisfied" "Very Dissatisfied" [61] "Very Dissatisfied" "Neutral" "Very Dissatisfied" [64] "Very Dissatisfied" "Very Satisfied" "Very Dissatisfied" [67] "Satisfied" "Very Dissatisfied" "Very Dissatisfied" [70] "Neutral" "Neutral" "Very Satisfied" [73] "Very Dissatisfied" "Very Dissatisfied" "Satisfied" [76] "Satisfied" "Very Satisfied" "Very Dissatisfied" [79] "Satisfied" "Neutral"