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
50 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,870 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
• 42,280 points
98 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
• 42,280 points
2,073 views
0 votes
2 answers

R Programming error in if condition

Hi. Instead of breaking the line add it ...READ MORE

answered Aug 26 in Data Analytics by anonymous
• 31,840 points
62 views
0 votes
2 answers

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

The same rules almost follow for all ...READ MORE

answered Aug 26 in Data Analytics by anonymous
• 31,840 points
108 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
29 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,870 points
151 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
29 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
34 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,870 points
301 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,870 points
49 views