2

I wrote this code and used

library('fastDummies'): 
New_Data <- dummy_cols(New_Curve_Data, select_columns = 'CountyName')

I just want the actual county name that is Banks to be displayed and not CountyName_Banks etc. There are like 100 dummy variables that I created. So I cant manually change the names.

Picture showing the column names that are being outputted

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Eena29
  • 23
  • 2

2 Answers2

2

The prefix substring in the column names can be removed with sub by matching the 'CountyName_' as pattern and replace it with blank ("") on the names of the 'New_Data'` and assign it back

names(New_Data) <- sub("CountyName_", "", names(New_Data))

This can be also done in base R with table

as.data.frame.matrix(table(seq_len(nrow(New_Curve_Data)), 
                      New_Curve_Data$CountyName))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You can use pivot_wider. Since you did not share an example data taking example from ?fastDummies::dummy_cols help page.

crime <- data.frame(city = c("SF", "SF", "NYC"),
                    year = c(1990, 2000, 1990),
                    crime = 1:3)

tidyr::pivot_wider(crime, names_from = city, values_from = city, 
                   values_fn = length, values_fill = 0)

#   year crime    SF   NYC
#  <dbl> <int> <int> <int>
#1  1990     1     1     0
#2  2000     2     1     0
#3  1990     3     0     1
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213