Removing the levels attribute in the output - R

10.9k views Asked by At

I am new to R Programming. I wrote a sample program and that returns a value of a particular column in a matrix. When I print the value i get something like this

[1] APPLE
2 Levels : 1 2 

How do I get only the value without the levels in the output.

Thanks in advance.

2

There are 2 answers

0
Rich Scriven On

You can print a factor without displaying the levels by using the max.levels argument in print().

Normal printing:

factor(letters[1:5])
# [1] a b c d e
# Levels: a b c d e

Levels removed:

print(factor(letters[1:5]), max.levels = 0)
# [1] a b c d e
0
pmagunia On

Just to expand on A5C1D2H2I1M1N2O1R2T1's comment, the following command is what prints the variable APPLE without all that levels stuff:

 as.character(APPLE)

To get help on the command within R type:

?as.character

Here is an online R help entry for the command:

https://stat.ethz.ch/R-manual/R-devel/library/base/html/character.html