Categorizing variable and make it a dummy in R
Your sample data does not include a variable called ESG.Kategorien
but it does include ESGscore
. The following should give you what you want:
Datensatz_final_so$Dummy <- cut(Datensatz_final_so$ESGscore, breaks=c(0, 25, 50, 75, 100), labels=c("poor", "medium", "good", "excellent"))
table(Datensatz_final_so$Dummy)
#
# poor medium good excellent
# 4 3 38 32
levels(Datensatz_final_so$Dummy)
# [1] "poor" "medium" "good" "excellent"
Note your original categorization places 75 in good AND excellent.
Let's take your data as df:
df<- structure(
list(
Company = c(
"AIR PRODUCTS & CHEMICALS INC",
...
...
),
row.names = c(NA,-77L),
class = c("tbl_df",
"tbl", "data.frame")
)
lets take categories and build a small dataframe, then a bit of dplyr
ESG <- c("poor", "medium", "good", "excellent")
da <- data.frame(ESGColumn = 1:4,FlatESG = ESG)
df <- df |> dplyr::mutate(ESGColumn = floor(ESGscore/25)+1) |>
dplyr::left_join(da, by="ESGColumn") |>
dplyr::select(-"ESGColumn")
head(df)
# A tibble: 6 × 5
Company Year gvkey ESGscore FlatESG
<chr> <dbl> <dbl> <dbl> <chr>
1 AIR PRODUCTS & CHEMICALS INC 2011 1209 84.3 excellent
2 AIR PRODUCTS & CHEMICALS INC 2012 1209 81.9 excellent
3 AIR PRODUCTS & CHEMICALS INC 2013 1209 77.4 excellent
4 AIR PRODUCTS & CHEMICALS INC 2014 1209 80.1 excellent
5 AIR PRODUCTS & CHEMICALS INC 2015 1209 78.6 excellent
6 AIR PRODUCTS & CHEMICALS INC 2016 1209 76.4 excellent
Grzegorz