How to print a factor vector without levels in R?


To print a factor vector without levels in R, we can follow the below steps −

  • First of all, create a factor vector.
  • Then, use as.character function to read the vector.

Example 1

Create a factor vector

Let’s create a vector called f1 as shown below −

 Live Demo

f1<-factor(sample(LETTERS[1:3],20,replace=TRUE))
f1

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

[1] A B B A C C C B C B B A A B B C B A A C
Levels: A B C

Print the factor vector without levels

Using as.character function to print factor vector f1 without levels −

 Live Demo

f1<-factor(sample(LETTERS[1:3],20,replace=TRUE))
as.character(f1)

Output

[1] "A" "B" "B" "A" "C" "C" "C" "B" "C" "B" "B" "A" "A" "B" "B" "C" "B" "A" "A"
[20] "C"

Example 2

Create a factor vector

Let’s create a vector called f2 as shown below −

 Live Demo

f2<-factor(sample(c("Male","Female"),120,replace=TRUE))
f2
[1] Male Male Female Female Female Female Male Female Female Male
[11] Male Female Female Female Female Male Female Female Female Female
[21] Male Male Male Female Female Female Female Male Male Female
[31] Female Male Male Male Male Male Male Male Male Female
[41] Female Male Male Female Male Male Male Female Female Male
[51] Female Male Female Male Female Male Female Female Male Female
[61] Male Male Female Female Male Female Female Male Female Male
[71] Male Male Female Female Female Male Female Female Female Male
[81] Female Male Male Male Male Female Male Male Female Male
[91] Male Male Female Male Female Male Male Female Male Female
[101] Male Female Female Female Male Male Male Male Male Female
[111] Male Female Male Female Female Male Female Female Male Male
Levels: Female Male

Print the factor vector without levels

Using as.character function to print factor vector f2 without levels −

 Live Demo

f2<-factor(sample(c("Male","Female"),120,replace=TRUE))
as.character(f2)

Output

[1] "Male" "Male" "Female" "Female" "Female" "Female" "Male" "Female"
[9] "Female" "Male" "Male" "Female" "Female" "Female" "Female" "Male"
[17] "Female" "Female" "Female" "Female" "Male" "Male" "Male" "Female"
[25] "Female" "Female" "Female" "Male" "Male" "Female" "Female" "Male"
[33] "Male" "Male" "Male" "Male" "Male" "Male" "Male" "Female"
[41] "Female" "Male" "Male" "Female" "Male" "Male" "Male" "Female"
[49] "Female" "Male" "Female" "Male" "Female" "Male" "Female" "Male"
[57] "Female" "Female" "Male" "Female" "Male" "Male" "Female" "Female"
[65] "Male" "Female" "Female" "Male" "Female" "Male" "Male" "Male"
[73] "Female" "Female" "Female" "Male" "Female" "Female" "Female" "Male"
[81] "Female" "Male" "Male" "Male" "Male" "Female" "Male" "Male"
[89] "Female" "Male" "Male" "Male" "Female" "Male" "Female" "Male"
[97] "Male" "Female" "Male" "Female" "Male" "Female" "Female" "Female"
[105] "Male" "Male" "Male" "Male" "Male" "Female" "Male" "Female"
[113] "Male" "Female" "Female" "Male" "Female" "Female" "Male" "Male"

Example 3

Create a factor vector

Let’s create a vector called f3 as shown below −

 Live Demo

f3<-factor(sample(c("Low","Medium","High"),120,replace=TRUE))
f3
[1] Medium High Medium High Medium High Low Low High Medium
[11] High Low High Medium Medium Low Medium Low High High
[21] Medium Medium Low Low High Low High Medium High Medium
[31] Low Low High High Low Low High Low Low High
[41] High Low High High Low High High Medium High Medium
[51] High Medium Low Medium Low Low High High High High
[61] Low Low High High Low Low High High High High
[71] Low Low Medium High Low Low Medium High Low Medium
[81] High Medium Medium Medium High Medium Low Medium Low High
[91] High Medium High Low Medium Low Low High Medium Low
[101] High Medium Low High High Low Medium Medium Medium Low
[111] High Low Medium High High High Medium Low Low Low
Levels: High Low Medium

Print the factor vector without levels

Using as.character function to print factor vector f3 without levels −

 Live Demo

f3<-factor(sample(c("Low","Medium","High"),120,replace=TRUE))
as.character(f3)

Output

[1] "Medium" "High" "Medium" "High" "Medium" "High" "Low" "Low"
[9] "High" "Medium" "High" "Low" "High" "Medium" "Medium" "Low"
[17] "Medium" "Low" "High" "High" "Medium" "Medium" "Low" "Low"
[25] "High" "Low" "High" "Medium" "High" "Medium" "Low" "Low"
[33] "High" "High" "Low" "Low" "High" "Low" "Low" "High"
[41] "High" "Low" "High" "High" "Low" "High" "High" "Medium"
[49] "High" "Medium" "High" "Medium" "Low" "Medium" "Low" "Low"
[57] "High" "High" "High" "High" "Low" "Low" "High" "High"
[65] "Low" "Low" "High" "High" "High" "High" "Low" "Low"
[73] "Medium" "High" "Low" "Low" "Medium" "High" "Low" "Medium"
[81] "High" "Medium" "Medium" "Medium" "High" "Medium" "Low" "Medium"
[89] "Low" "High" "High" "Medium" "High" "Low" "Medium" "Low"
[97] "Low" "High" "Medium" "Low" "High" "Medium" "Low" "High"
[105] "High" "Low" "Medium" "Medium" "Medium" "Low" "High" "Low"
[113] "Medium" "High" "High" "High" "Medium" "Low" "Low" "Low"

Example 4

Create a factor vector

Let’s create a vector called f4 as shown below −

 Live Demo

f4<-factor(sample(c("First","Second","Third","Fourth","Fifth"),120,replace=TRUE))
f4
[1] Fifth Fifth First Second Fourth Fifth Second Second Fourth Second
[11] First Third Fifth Fourth Third First Third Third Third First
[21] Fourth Second Second Third Second Fourth Third Fourth First First
[31] Fifth Second Third Second Fourth Fourth Fourth Third Fourth First
[41] First Second Second Second Third Fifth Fifth Second Third Fifth
[51] Second Third Second First First Third Fifth Third Third Third
[61] Second Third Fifth Third Fifth Fifth Third Third Fourth First
[71] First Second Second Fourth Third Fifth Third First Third Third
[81] Fifth Fifth Third Third Fourth First First Fifth First Fifth
[91] Second Fifth Fourth First Fifth Fourth Second Third Second Fifth
[101] First Fourth Second Fourth Fifth Fifth Fifth Fourth Fourth Fourth
[111] Fourth First Fourth First Fourth Fourth First Fifth Second Third
Levels: Fifth First Fourth Second Third

Print the factor vector without levels

Using as.character function to print factor vector f4 without levels −

 Live Demo

f4<-factor(sample(c("First","Second","Third","Fourth","Fifth"),120,replace=TRUE))
as.character(f4)

Output

9] "Fourth" "Second" "First" "Third" "Fifth" "Fourth" "Third" "First"
[17] "Third" "Third" "Third" "First" "Fourth" "Second" "Second" "Third"
[25] "Second" "Fourth" "Third" "Fourth" "First" "First" "Fifth" "Second"
[33] "Third" "Second" "Fourth" "Fourth" "Fourth" "Third" "Fourth" "First"
[41] "First" "Second" "Second" "Second" "Third" "Fifth" "Fifth" "Second"
[49] "Third" "Fifth" "Second" "Third" "Second" "First" "First" "Third"
[57] "Fifth" "Third" "Third" "Third" "Second" "Third" "Fifth" "Third"
[65] "Fifth" "Fifth" "Third" "Third" "Fourth" "First" "First" "Second"
[73] "Second" "Fourth" "Third" "Fifth" "Third" "First" "Third" "Third"
[81] "Fifth" "Fifth" "Third" "Third" "Fourth" "First" "First" "Fifth"
[89] "First" "Fifth" "Second" "Fifth" "Fourth" "First" "Fifth" "Fourth"
[97] "Second" "Third" "Second" "Fifth" "First" "Fourth" "Second" "Fourth"
[105] "Fifth" "Fifth" "Fifth" "Fourth" "Fourth" "Fourth" "Fourth" "First"
[113] "Fourth" "First" "Fourth" "Fourth" "First" "Fifth" "Second" "Third"

Updated on: 14-Aug-2021

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements