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 28, 2018 in Data Analytics by Tyrion anex
• 8,700 points
722 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 28, 2018 by Sophie may
• 10,620 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
• 52,350 points
2,531 views
+2 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
• 52,350 points
9,563 views
0 votes
2 answers

R Programming error in if condition

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

answered Aug 26, 2019 in Data Analytics by anonymous
• 33,030 points
1,058 views
+4 votes
0 answers

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

Jun 24, 2020 in Data Analytics by Molly
• 160 points
1,025 views
+1 vote
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,700 points
658 views
+1 vote
1 answer

R Programming: Market Basket Analysis Error

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

answered Feb 12, 2019 in Data Analytics by Sophie may
• 10,620 points
1,507 views
+1 vote
1 answer

R Programming: regexpr error

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

answered Feb 21, 2019 in Data Analytics by Tyrion anex
• 8,700 points
625 views
+1 vote
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, 2019 in Data Analytics by Tyrion anex
• 8,700 points
601 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
• 10,620 points
3,251 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
• 10,620 points
693 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP