如何在 R 向量的调查中用序数字符串替换数字?


替换数字用序数字符串的最简单方法是使用 ifelse 函数。R 中的 ifelse 函数的作用是 ifelse(test_expression, x, y)。其中,test_expression 必须为逻辑向量,或可强制转换为逻辑类型。返回值是与 test_expression 长度相同的向量。

示例 1

在线演示

> 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"

示例 2

在线演示

> 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"

示例 3

在线演示

> 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"

示例 4

在线演示

> 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"

更新于:19-11-2020

1K+ 浏览次数

开启你的 职业 生涯

按要求完成课程并获得认证

立即开始
广告