I'm in hour 7 of writing this book today, because deadline, which is certainly a problem, and I am not thinking straight. I'm also not sure if I should ask this question here, or start somewhere else. I have tried searching to make sure I'm not duplicating another question, but everything I read is either people putting incorrect objects on the level() function (like a data frame) or technical discussions that I can't follow (and don't seem to apply to me).
It's also so elementary I'm a little ashamed to ask.
Installed:
library(tidyverse)
library(datatables)
library(forcats)
These all work great:
View (chickwts)
names (chickwts)
[1] "weight" "feed"
levels (chickwts$feed)
[1] "casein" "horsebean" "linseed" "meatmeal" "soybean" "sunflower"
I have an artificial data set whose dput() I'll attach below. The filename is M&Ms.csv, with two variables: M&Ms and Brand X. 250 rows of colors, headed toward a test whether the two brands adhere to the M&Ms published proportion.
I load the data in using readr, using Rstudio's menu.
names(M_Ms)
[1] "M&Ms" "BrandX"
levels(M_Ms$M&Ms)
NULL
levels(M_Ms$BrandX)
NULL
Why the NULLs? I can't see a difference between the two data sets.
I've read ?levels and ?readr and the only thing I see is the following.
x: an object, for example, a factor.
value: A valid value forlevels(x). For the default method,NULLor a character vector.
By being inside the datasets package(?), chickwt is returning its variables' levels, and M&Ms isn't. By using readr, I'm making a tibble, which I thought was an extension of a data.frame, so I'm not sure why levels doesn't work.
Even though these aren't really factor variables, do I need to make them factors (like Chapter 15 in R for Data Science?). Somehow I thought tidyverse lifted that burden from me.
structure(list(M&Ms = c("brown", "red", "red", "blue", "green",
"red", "red", "orange", "green", "orange", "brown", "green",
"yellow", "green", "blue", "yellow", "brown", "yellow", "red",
"green", "brown", "yellow", "yellow", "green", "yellow", "brown",
"brown", "blue", "blue", "brown", "blue", "yellow", "yellow",
"blue", "brown", "yellow", "blue", "orange", "yellow", "blue",
"red", "red", "red", "red", "blue", "yellow", "blue", "blue",
"brown", "yellow", "red", "red", "orange", "brown", "brown",
"orange", "brown", "brown", "yellow", "yellow", "red", "red",
"brown", "red", "blue", "red", "yellow", "brown", "orange", "red",
"red", "orange", "brown", "red", "yellow", "green", "brown",
"blue", "red", "brown", "brown", "brown", "red", "yellow", "brown",
"brown", "brown", "brown", "red", "blue", "brown", "red", "orange",
"brown", "brown", "blue", "brown", "green", "green", "brown",
"red", "brown", "brown", "red", "green", "red", "brown", "yellow",
"yellow", "yellow", "yellow", "brown", "red", "red", "orange",
"red", "brown", "red", "brown", "green", "blue", "yellow", "red",
"blue", "red", "red", "green", "red", "orange", "red", "brown",
"green", "orange", "red", "orange", "red", "yellow", "brown",
"yellow", "yellow", "blue", "brown", "brown", "yellow", "green",
"brown", "red", "brown", "yellow", "orange", "red", "red", "brown",
"green", "green", "brown", "brown", "yellow", "orange", "red",
"red", "yellow", "green", "yellow", "green", "red", "red", "brown",
"brown", "yellow", "green", "orange", "orange", "red", "blue",
"brown", "brown", "yellow", "blue", "yellow", "red", "brown",
"red", "red", "brown", "blue", "red", "yellow", "blue", "blue",
"yellow", "yellow", "brown", "yellow", "brown", "yellow", "brown",
"yellow", "brown", "blue", "blue", "orange", "green", "red",
"red", "red", "brown", "red", "red", "green", "yellow", "green",
"red", "red", "blue", "green", "blue", "brown", "green", "green",
"red", "red", "red", "yellow", "brown", "green", "yellow", "blue",
"red", "red", "yellow", "green", "brown", "yellow", "yellow",
"yellow", "yellow", "red", "blue", "green", "brown", "orange",
"yellow", "orange", "yellow", "brown", "orange", "red", "yellow",
"red"), BrandX = c("yellow", "red", "orange", "blue", "orange",
"green", "red", "yellow", "blue", "blue", "red", "red", "yellow",
"blue", "yellow", "orange", "red", "green", "brown", "brown",
"red", "red", "yellow", "yellow", "brown", "red", "blue", "orange",
"red", "red", "yellow", "orange", "yellow", "orange", "brown",
"blue", "blue", "green", "red", "orange", "brown", "orange",
"red", "red", "brown", "red", "yellow", "yellow", "brown", "orange",
"blue", "brown", "yellow", "red", "red", "brown", "brown", "yellow",
"brown", "blue", "orange", "orange", "brown", "orange", "yellow",
"orange", "red", "red", "blue", "red", "blue", "red", "blue",
"orange", "red", "orange", "red", "red", "brown", "brown", "brown",
"red", "yellow", "red", "blue", "yellow", "blue", "brown", "orange",
"yellow", "red", "orange", "green", "brown", "red", "orange",
"yellow", "red", "brown", "orange", "yellow", "orange", "yellow",
"brown", "blue", "brown", "red", "yellow", "yellow", "red", "brown",
"yellow", "brown", "orange", "brown", "blue", "red", "orange",
"yellow", "orange", "blue", "red", "blue", "red", "orange", "red",
"red", "green", "brown", "brown", "red", "brown", "red", "brown",
"yellow", "brown", "orange", "yellow", "brown", "orange", "red",
"red", "red", "red", "orange", "green", "brown", "yellow", "red",
"brown", "brown", "red", "yellow", "orange", "red", "blue", "blue",
"yellow", "brown", "yellow", "blue", "brown", "red", "orange",
"brown", "brown", "green", "brown", "blue", "red", "brown", "brown",
"brown", "blue", "yellow", "orange", "yellow", "yellow", "yellow",
"orange", "orange", "brown", "red", "brown", "orange", "yellow",
"red", "red", "yellow", "blue", "red", "green", "brown", "orange",
"red", "brown", "orange", "yellow", "yellow", "brown", "brown",
"red", "blue", "yellow", "red", "brown", "blue", "brown", "red",
"green", "yellow", "yellow", "red", "orange", "brown", "red",
"orange", "red", "red", "red", "brown", "blue", "yellow", "red",
"red", "brown", "red", "brown", "blue", "orange", "orange", "yellow",
"red", "red", "brown", "brown", "yellow", "orange", "brown",
"orange", "brown", "red", "yellow", "orange", "red", "brown",
"red", "orange", "yellow", "red")), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -250L), spec = structure(list(
cols = list(M&Ms = structure(list(), class = c("collector_character",
"collector")), BrandX = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
= bunny. Later on, you might extend/expand this label into a category, to understand that 