# How to replace numbers with ordinal strings for a survey in an R vector?

The easiest way to replace numbers with ordinal strings is using ifelse function. The ifelse function in R works as ifelse(test_expression, x, y). Here, test_expression must be a logical vector or an object that can be coerced to logical). The return value is a vector with the same length as test_expression.

## Example1

Live Demo

> x1<-sample(0:1,100,replace=TRUE)
> x1

## Output

[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

## Example

> x1<-ifelse(x1==0,"No","Yes")
> x1

## Output

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

## Example2

Live Demo

> x2<-sample(-1:1,100,replace=TRUE)
> x2

## Output

 [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

## Example

> x2<-ifelse(x2==-1,"Low",ifelse(x2==0,"Medium","High"))
> x2

## Output

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

## Example3

Live Demo

> x3<-sample(1:3,100,replace=TRUE)
> x3

## Output

[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

## Example

> x3<-ifelse(x3==1,"First",ifelse(x3==2,"Second","Third"))
> x3

## Output

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

## Example4

Live Demo

> x4<-sample(1:5,80,replace=TRUE)
> x4

## Output

[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

## Example

> x4<-ifelse(x4==1,"Very Satisfied",ifelse(x4==2,"Satisfied",ifelse(x4==3,"Neutral",ifelse(x4==3,"Dissatisfied","Very Dissatisfied"))))
> x4

## Output

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