Counting depth or the deepest level a nested list goes to
Here is one way to write the function depth = lambda L: isinstance(L, list) and max(map(depth, L))+1 I think the idea you are missing is to use max()
Here is one way to write the function depth = lambda L: isinstance(L, list) and max(map(depth, L))+1 I think the idea you are missing is to use max()
Assuming your dataframe is mydf: mydf$task <- factor(mydf$task, levels = c(“up”, “down”, “left”, “right”, “front”, “back”))
The answers here are good, but they are missing an important point. Let me try and describe it. R is a functional language and does not like to mutate its objects. But it does allow assignment statements, using replacement functions: levels(x) <- y is equivalent to x <- `levels<-`(x, y) The trick is, this rewriting … Read more
You need to set drop=FALSE on both scales (fill and x) like this: library(ggplot2) df <- data.frame(type=c(“A”, “A”, “A”, “B”, “B”), group=rep(“group1”, 5)) df1 <- data.frame(type=c(“A”, “A”, “A”, “B”, “B”, “A”, “A”, “C”, “B”, “B”), group=c(rep(“group1”, 5),rep(“group2”, 5))) df$type <- factor(df$type, levels=c(“A”,”B”, “C”)) df1$type <- factor(df1$type, levels=c(“A”,”B”, “C”)) plt <- ggplot(df, aes(x=type, fill=type)) + geom_bar(position=’dodge’) … Read more
Use the levels argument of factor: df <- data.frame(f = 1:4, g = letters[1:4]) df # f g # 1 1 a # 2 2 b # 3 3 c # 4 4 d levels(df$g) # [1] “a” “b” “c” “d” df$g <- factor(df$g, levels = letters[4:1]) # levels(df$g) # [1] “d” “c” “b” “a” … Read more