1

I am stuck on a question in R, i need to sum a list of characters from the starwars dataset, which is the starwars eye color. The question asks how many different eye colours the character have. The answer is 15, which i derived from a table (table(starwars$eyecolor), but i cannot figure the code to get to 15.

Chad
  • 21
  • 2

2 Answers2

1
length(unique(starwars$eye_color))
dy_by
  • 1,061
  • 1
  • 4
  • 13
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Gerhard Jul 30 '21 at 12:10
1

Try any of below

> length(levels(factor(starwars$eye_color)))
[1] 15

> length(unique(starwars$eye_color))
[1] 15

> sum(!duplicated(starwars$eye_color))
[1] 15
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81