Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

Loop For in R returns Null(empty) values instead of expected

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?

question from:https://stackoverflow.com/questions/65893673/loop-for-in-r-returns-nullempty-values-instead-of-expected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...