How to handle categorical variables in the below two scenarios with respect to Multi Linear Regression Models.

If we have a categorical variable (X1) which consists of 100 factor levels (e.g. Name of Cities in Country), How will we convert them into dummy variables. I understand that we should not create 100 dummy variables (DX1, DX2, …, DX99) to represent these categorical values. How will we handle this situation?

Let’s consider a scenario where we have a categorical variable (X1) which consists of 4 factor levels, what would be the code in R to create the dummy variables for this categorical variable?