0

I'm new to R and am trying to create custom values in the 'Stats / Values' column in R. I use the dfSummary function as follows:

dfSummary(as.data.frame(y), headings = FALSE, varnumbers = FALSE, labels.col = TRUE, max.distinct.values = 25))}

Example output I get in the 'Stats / Values' column is 1. Male, 2. Female.

How can I customise this column? I have a list called index and text. e.g. index = [0,1], text = ['Male','Female']. Based on this data I would like the 'Stats / Values' column to be 0. Male, 1. Female.

A second example: index = [0,1,9], text = ['Car','Truck', 'Plane']. 'Stats / Values' column should be 0. Car, 1. Truck, 9. Plane.

What is a simple way to do this?

Mark
  • 7,785
  • 2
  • 14
  • 34
mr_js
  • 949
  • 12
  • 29
  • it seems like `dfSummary` is quite limited. You can change the Stats/Values column to add other numerical calculations, but nothing like this. Are you wedded to the idea of using `dfSummary`, or would other things be okay? – Mark Jul 11 '23 at 14:25
  • Any package or code that gets the job done would be sufficient. It doesn't have to be dfSummary. – mr_js Jul 11 '23 at 17:16

2 Answers2

1

You could change the levels of the string variable beforehand:

d <- data.frame(index = c(0,1,9), text = c('Car','Truck', 'Plane'))
d$text = with(d, sprintf('%s..%s', index, text))

dfSummary(d)

output (cropped):

-----------------------------------------------------
No   Variable      Stats / Values          Freqs (% o
---- ------------- ----------------------- ----------

2    text          1. 0..Car              1 (33.3%) 
     [character]   2. 1..Truck            1 (33.3%) 
                   3. 9..Plane            1 (33.3%) 
-----------------------------------------------------

However, I couldn't find an option to turn off the enumeration of levels, but see yourself: ?st_options

I_O
  • 4,983
  • 2
  • 2
  • 15
0

Not entirely clear to me what you want, but here are some options:

df <- df %>% transmute(`Stats / Values`= paste0(index,". ", text))

Using gt:

df %>% gt()

enter image description here

Using the asciify() function from this answer by Gavin Simpson:

df %>% asciify()

+----------------+
| Stats / Values |
+----------------+
|         0. car |
+----------------+
|       1. truck |
+----------------+
|       9. plane |
+----------------+

Or keeping it as a dataframe, and just printing it:

df
  Stats / Values
1         0. car
2       1. truck
3       9. plane
Mark
  • 7,785
  • 2
  • 14
  • 34