How to Order a Nominale Variable. E.G Month in R

How do you order a nominale variable. e.g month in R?

@PhilipPham's answer is correct: this is equivalent but a little simpler:

Month <- factor(Month,levels=month.name)

since there is a built-in month.name variable in R that gives the English month names in order.

Arrange the labels of monthly time series in ggplot, R

Setting month as a factor and specifying the order of the levels (with the argument levels) solves the issue, as below.

month<-c("May","June","July","August","September","May","June","July","August","September")
year<-c("1985","1985","1985","1985","1985","1986","1986","1986","1986","1986")

value<-c(2,6,4,5,9,7,2,6,5,4)

df<-data.frame(month,year,value)

df$month <- factor(df$month, levels = c("May", "June", "July", "August", "September"))

ggplot(df, aes(x=month, y=value,group=year))+theme_bw()+theme(plot.background = element_blank(),panel.grid.major = element_blank()
,panel.grid.minor = element_blank()) +ylim(0,10)+labs(x="Month",y=expression(bold(paste("variable here"))))+
theme(axis.title.x = element_text(face="bold", size=12),axis.text.x = element_text(size=9))+
theme(axis.title.y = element_text(face="bold", size=12),axis.text.y = element_text(size=9))+theme(axis.text.x = element_text(angle = 45, hjust = 1))+
geom_line(linetype="solid", size=1)+geom_point(color="gray7", size=2,shape=15)

Recoding a nominal variable with multiple categories into a dummy variable

Reproducible data and expected output would be very useful, but it looks like your ifelse() statement hasn't been constructed properly, and could be simplified:

anesnew <- anes %>%
filter(!is.na(pid_x), pid_x != 4) %>%
mutate(party_id_recode = case_when(pid_x < 4 ~ 1,
pid_x > 4 ~ 0))

With the following sample data:

anes <- tibble(pid_x = c(1, 2, 3, 4, 5, 6, 7, NA))

The results are:

# A tibble: 6 x 2
pid_x party_id_recode
<dbl> <dbl>
1 1 1
2 2 1
3 3 1
4 5 0
5 6 0
6 7 0

Converting factor / ?nominal variables into numeric in R

You could try

c(17,8,4)[as.numeric(eduyears1994)]
#[1] 17 4 17 4 17 17 4 4 17 17 17 17 4 8 4 4 8 4 8 8

or

 unname(c('4 lata/1'=4, '2'=8, '17 lat' =17)[as.character(eduyears1994)])
#[1] 17 4 17 4 17 17 4 4 17 17 17 17 4 8 4 4 8 4 8 8

If 8 was infact a typo, you could use

 library(stringi)
as.numeric(unlist(stri_extract_all_regex(eduyears1994, '^\\d+')))
#[1] 17 4 17 4 17 17 4 4 17 17 17 17 4 2 4 4 2 4 2 2

data

set.seed(21)
eduyears1994 <- factor(sample(c('4 lata/1', 2, '17 lat'), 20, replace=TRUE))

Plot multiple nominal variables

We can use tidyverse to create a tbl of the information you want and then graph it.

library(tidyverse)

df2 <- gather(df, "website", "aware", 3:7, factor_key = T) %>%
group_by(website, aware) %>%
summarize(n = n()) %>%
ungroup() %>%
filter(aware == "Yes") %>%
complete(website, fill = list(n = 0))

ggplot(data = df2) +
geom_bar(aes(website, n), stat = "identity")

How to reorder a numeric x-axis in ggplot2?

You could reorder your months numerically then add labels in the correct order:

library(ggplot2)

x <- seq(1, 12, 1)
set.seed(2022)
y <- rnorm(n = 12)

df <- data.frame("x" = x, "y" = y)

df$x <- 1 +(df$x+5) %% 12

tile.df <- data.frame(
"x" = seq(1, 12, by = 1/12), # Note how the color scale is much higher resolution than the data
"y" = -4
)

ggplot(data = df, aes(x = x, y = y))+
theme_classic()+
geom_line()+
scale_x_continuous(breaks = seq(1, 12, 1), labels = c(month.abb[7:12], month.abb[1:6]))+
scale_fill_gradient2(low = "black", mid = "gray", high = "white", midpoint = 6)+
theme(legend.position = "none")+
geom_tile(data = tile.df, aes(y = y, fill = x), height = 0.5)

Sample Image

The line df$x <- 1 +(df$x+5) %% 12 is behind the scenes reordering your months so that July = 1 to be plotted, then the labels of the axis show months in the new order.

A more intuitive way may be to convert to a factor, put in the order you want, then convert back to an integer when plotting (whilst similarly adding the correctly ordered labels:

reordered_months <- c(month.abb[7:12], month.abb[1:6])

df$month <- factor(month.abb[df$x], levels = reordered_months)

ggplot(data = df, aes(x = as.numeric(month), y = y)) +
theme_classic()+
geom_line()+
scale_x_continuous(breaks = seq(1, 12, 1), labels = reordered_months)+
scale_fill_gradient2(low = "black", mid = "gray", high = "white", midpoint = 6)+
theme(legend.position = "none")+
geom_tile(data = tile.df, aes(y = y, x = x, fill = x), height = 0.5)

(plots the same graph)

Created on 2022-05-12 by the reprex package (v2.0.1)

R - Change column variable from categorical value to nominal

You can use X to subset your categorical vector.

dataset$X <- c("Low","Medium Low","Medium High","High")[dataset$X]
dataset
# X
#1 Low
#2 Medium Low
#3 Medium High
#4 High

Data:

dataset <- data.frame(X=1:4)

how to deal with a model with multilevel nominal dependent variable, multilevel ordered independent variable and random term?

If I'm understanding you correctly, the implementation in mlogit should be what you are looking for. This package is designed to implement modeling for multinomial outcomes. Check out ?mlogit::mlogit for details and examples (the "mixed" model as it is sometimes referred to), but your specification would be:

m <- mlogit(A ~ B | district, data=mydata)


Related Topics



Leave a reply



Submit