0

I want to create variables which contain the description of the variables in my data, so I have tried to create loop.

for (i in 1:ncol(data)) {
   nam <- paste("descr", colnames(data[i]), sep = "_")
   des = descr(data[,i])
   assign(nam, des)
}

I can see the proper names of variables in the environment and the correct output under the chunk, but the value of my variables is always NULL (empty).

Is it possible to save both name and value?

Maria
  • 1
  • Hi Maria, can you post a subset of your data to make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? It'd help us debug if we know what the `descr()` function and `dput(head(data))` look like. – Dubukay Jan 26 '21 at 01:37
  • It actually works with any data, I guess. At least now with sjmisc it started to work correctly. – Maria Jan 26 '21 at 10:52

1 Answers1

0

The following code works for me when testing on mtcars dataset.

library(sjmisc)
data <- mtcars
for (i in 1:ncol(data)) {
  nam <- paste("descr", colnames(data[i]), sep = "_")
  des = descr(data[,i])
  assign(nam, des)
}

descr_disp

descr_disp

## Basic descriptive statistics

# var    type label  n NA.prc   mean     sd    se    md trimmed            range     iqr skew
#  dd numeric    dd 32      0 230.72 123.94 21.91 196.3  222.52 400.9 (71.1-472) 205.175 0.42

descr_carb

## Basic descriptive statistics

# var    type label  n NA.prc mean   sd   se md trimmed   range iqr skew
#  dd numeric    dd 32      0 2.81 1.62 0.29  2    2.65 7 (1-8)   2 1.16

However, do you really want to create multiple such vectors when descr function works on dataframes.

descr(data)

## Basic descriptive statistics

#  var    type label  n NA.prc   mean     sd    se     md trimmed            range       iqr  skew
#  mpg numeric   mpg 32      0  20.09   6.03  1.07  19.20   19.70 23.5 (10.4-33.9)   7.37500  0.67
#  cyl numeric   cyl 32      0   6.19   1.79  0.32   6.00    6.23          4 (4-8)   4.00000 -0.19
# disp numeric  disp 32      0 230.72 123.94 21.91 196.30  222.52 400.9 (71.1-472) 205.17500  0.42
#   hp numeric    hp 32      0 146.69  68.56 12.12 123.00  141.19     283 (52-335)  83.50000  0.80
# drat numeric  drat 32      0   3.60   0.53  0.09   3.70    3.58 2.17 (2.76-4.93)   0.84000  0.29
#   wt numeric    wt 32      0   3.22   0.98  0.17   3.33    3.15 3.91 (1.51-5.42)   1.02875  0.47
# qsec numeric  qsec 32      0  17.85   1.79  0.32  17.71   17.83  8.4 (14.5-22.9)   2.00750  0.41
#   vs numeric    vs 32      0   0.44   0.50  0.09   0.00    0.42          1 (0-1)   1.00000  0.26
#   am numeric    am 32      0   0.41   0.50  0.09   0.00    0.38          1 (0-1)   1.00000  0.40
# gear numeric  gear 32      0   3.69   0.74  0.13   4.00    3.62          2 (3-5)   1.00000  0.58
# carb numeric  carb 32      0   2.81   1.62  0.29   2.00    2.65          7 (1-8)   2.00000  1.16
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213