levels

How can I drop unused levels from a data frame?

こ雲淡風輕ζ 提交于 2019-11-27 04:15:36
问题 Given the following mock data: set.seed(123) x <- data.frame(let = sample(letters[1:5], 100, replace = T), num = sample(1:10, 100, replace = T)) y <- subset(x, let != 'a') Creating a table of y$let yields a b c d e 0 20 21 22 18 But I don't want a to show anymore. If I try to do this: levels(y$let) <- factor(y$let) I mess the frequencies, since now table(y$let) gives me b d c e 0 20 21 40 I'm aware I could do xtabs(~ y$let, drop.unused.levels = T) and work around the problem, but it doesn't

ggplot2 keep unused levels barplot

社会主义新天地 提交于 2019-11-26 16:35:34
I want to plot unused levels (that is, levels where the count is 0) in my bar-plot, however, unused levels are dropped and I cannot figure out how to keep them df <- data.frame(type=c("A", "A", "A", "B", "B"), group=rep("group1", 5)) df$type <- factor(df$type, levels=c("A","B", "C")) ggplot(df, aes(x=group, fill=type)) + geom_bar() In the above example, I want to see C plotted with a count of 0, but it is completely absent... Thanks for any help Ulrik Edit: This does what I want df <- data.frame(type=c("A", "A", "A", "B", "B"), group=rep("group1", 5)) df1 <- data.frame(type=c("A", "A", "A", "B

`levels<-`( What sorcery is this?

感情迁移 提交于 2019-11-26 11:04:17
In an answer to another question, @Marek posted the following solution: https://stackoverflow.com/a/10432263/636656 dat <- structure(list(product = c(11L, 11L, 9L, 9L, 6L, 1L, 11L, 5L, 7L, 11L, 5L, 11L, 4L, 3L, 10L, 7L, 10L, 5L, 9L, 8L)), .Names = "product", row.names = c(NA, -20L), class = "data.frame") `levels<-`( factor(dat$product), list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12) ) Which produces as output: [1] Generic Generic Bayer Bayer Advil Tylenol Generic Advil Bayer Generic Advil Generic Advil Tylenol [15] Generic Bayer Generic Advil Bayer Bayer This is just the printout of a

Why is the terminology of labels and levels in factors so weird?

橙三吉。 提交于 2019-11-26 10:31:31
问题 An example of a non-settable function would be labels . You can only set factor labels when they are created with the factor function. There is no labels<- function. Not that \'labels\' and \'levels\' in factors make any sense.... > fac <- factor(1:3, labels=c(\"one\", \"two\", \"three\")) > fac [1] one two three Levels: one two three > labels(fac) [1] \"1\" \"2\" \"3\" OK, I asked for labels, which one might assume were as set by the factor call, but I get something quite ... what\'s the

ggplot2 keep unused levels barplot

半城伤御伤魂 提交于 2019-11-26 04:51:01
问题 I want to plot unused levels (that is, levels where the count is 0) in my bar-plot, however, unused levels are dropped and I cannot figure out how to keep them df <- data.frame(type=c(\"A\", \"A\", \"A\", \"B\", \"B\"), group=rep(\"group1\", 5)) df$type <- factor(df$type, levels=c(\"A\",\"B\", \"C\")) ggplot(df, aes(x=group, fill=type)) + geom_bar() In the above example, I want to see C plotted with a count of 0, but it is completely absent... Thanks for any help Ulrik Edit: This does what I

`levels<-`( What sorcery is this?

邮差的信 提交于 2019-11-26 01:57:35
问题 In an answer to another question, @Marek posted the following solution: https://stackoverflow.com/a/10432263/636656 dat <- structure(list(product = c(11L, 11L, 9L, 9L, 6L, 1L, 11L, 5L, 7L, 11L, 5L, 11L, 4L, 3L, 10L, 7L, 10L, 5L, 9L, 8L)), .Names = \"product\", row.names = c(NA, -20L), class = \"data.frame\") `levels<-`( factor(dat$product), list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12) ) Which produces as output: [1] Generic Generic Bayer Bayer Advil Tylenol Generic Advil Bayer

Reorder levels of a factor without changing order of values

倾然丶 夕夏残阳落幕 提交于 2019-11-26 00:46:54
问题 I have data frame with some numerical variables and some categorical factor variables. The order of levels for those factors is not the way I want them to be. numbers <- 1:4 letters <- factor(c(\"a\", \"b\", \"c\", \"d\")) df <- data.frame(numbers, letters) df # numbers letters # 1 1 a # 2 2 b # 3 3 c # 4 4 d If I change the order of the levels, the letters no longer are with their corresponding numbers (my data is total nonsense from this point on). levels(df$letters) <- c(\"d\", \"c\", \"b\