I am new to R. I'm facing a problem when I do the following:

fert <- as.factor(c(50,20,10,10,20,50))
levels(fert)

This is what I get and it makes sense to me:

[1] "10" "20" "50"

When I run this:

levels(fert)[fert]

I get this:

"50" "20" "10" "10" "20" "50"

I don't understand what this is, I thought it was the definition of fert.

Dec 28, 2018 503 views

## 1 answer to this question.

You're using a factor:

fert <-  factor(c(50,20,10,10,20,50))
levels(fert)
#[1] "10" "20" "50"

Factors are always in a sequential form with labels:

as.numeric(fert)
#[1] 3  2  1  1  2  3
#  corresponding to the labels of:
#   50 20 10 10 20 50

That's why:

levels(fert)[c(3,2,1,1,2,3)]
#[1] "50" "20" "10" "10" "20" "50"

You get this:

levels(fert)[fert]
#[1] "50" "20" "10" "10" "20" "50"
• 10,610 points

## "no applicable method" Error in r programming

This is caused by using an object-oriented ...READ MORE

## “subscript out of bounds” Error in r programming

This error is likely to occur when ...READ MORE

## R programming Error: Error in if (d >= 0) { : missing value where TRUE/FALSE needed

Here is my code: for(i in 1:(nrow(moon_sub))){   l_df[i,] <- ...READ MORE

+1 vote

## R programming error

Alright, you can either use gsub to match the ...READ MORE

+1 vote

+1 vote

## R Programming: regexpr error

The below code will help: gregexpr("D", x) # [[1]] # ...READ MORE

+1 vote

## R programming: Graphs

Try this: ggplot(dfc, aes(x=x, y=y, colour=f, fill=f, ymin=y-se, ...READ MORE