R Programming error in 'fert'

0 votes

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 27, 2018 in Data Analytics by Tyrion anex
• 8,280 points
37 views

1 answer to this question.

0 votes

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"
answered Dec 27, 2018 by Sophie may
• 9,430 points

Related Questions In Data Analytics

0 votes
1 answer

"no applicable method" Error in r programming

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

answered Oct 31, 2018 in Data Analytics by Kalgi
• 37,320 points
44 views
0 votes
1 answer

“subscript out of bounds” Error in r programming

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

answered Oct 31, 2018 in Data Analytics by Kalgi
• 37,320 points
928 views
0 votes
1 answer

R Programming error in if condition

There's a small mistake in your syntax. This ...READ MORE

answered Jan 18 in Data Analytics by Tyrion anex
• 8,280 points
47 views
0 votes
1 answer

What are the rules to define a variable name in R programming language?

A variable name in R programming language ...READ MORE

answered Sep 7, 2018 in Data Analytics by zombie
• 3,690 points
69 views
0 votes
1 answer

R programming error

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

answered Dec 18, 2018 in Data Analytics by Tyrion anex
• 8,280 points
16 views
0 votes
1 answer

R Programming: Market Basket Analysis Error

The basket.sorted() has less than 5 rules. Refer ...READ MORE

answered Feb 11 in Data Analytics by Sophie may
• 9,430 points
87 views
0 votes
1 answer

R Programming: regexpr error

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

answered Feb 21 in Data Analytics by Tyrion anex
• 8,280 points
21 views
0 votes
1 answer

R programming: Graphs

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

answered Feb 21 in Data Analytics by Tyrion anex
• 8,280 points
18 views
0 votes
1 answer

R programming: Unexpected symbol error

Format your code this way: myfunction <- function() ...READ MORE

answered Dec 17, 2018 in Data Analytics by Sophie may
• 9,430 points
161 views
0 votes
1 answer

Can we have an if loop inside a for loop in R programming?

You're If loop doesn't have any condition ...READ MORE

answered Dec 21, 2018 in Data Analytics by Sophie may
• 9,430 points
39 views