如何在R中添加变量描述?
要在R中添加变量描述,我们可以使用注释函数,如果我们想看一下描述,那么将使用数据帧的结构调用。例如,如果我们有一个数据框说df包含x列,那么我们可以使用命令comment(df$x)<-c(“此变量的名称为x”)来描述x。现在来看一下,我们可以使用str(df)。
例1
考虑以下数据帧-
x1<-rnorm(20,21,3.24) x2<-rnorm(20,5,2.1) df1<-data.frame(x1,x2) df1输出结果
x1 x2 1 23.12252 5.085650 2 19.81415 5.180194 3 14.41423 2.756885 4 21.34914 5.714200 5 18.34662 3.814034 6 21.37762 9.538720 7 21.48240 4.838389 8 22.58701 2.185943 9 15.68257 5.084348 10 20.86272 5.732107 11 19.84529 3.430304 12 26.23832 3.684373 13 25.35179 5.001748 14 19.83831 3.393473 15 23.57819 5.057545 16 15.69374 7.442210 17 15.69028 6.722865 18 18.94718 8.046787 19 25.26722 2.776823 20 23.32905 3.561213
str(df1)
'data.frame': 20 obs. of 2 variables: $ x1: num 23.1 19.8 14.4 21.3 18.3 ... $ x2: num 5.09 5.18 2.76 5.71 3.81 ...
在df1中添加变量的描述-
comment(df1$x1)<-c("This variable follows normal distribution") comment(df1$x2)<-c("This variable follows normal distribution") str(df1)
'data.frame':20磅。2个变量-
$ x1: num 23.1 19.8 14.4 21.3 18.3 ... ..- attr(*, "comment")= chr "This variable follows normal distribution" $ x2: num 5.09 5.18 2.76 5.71 3.81 ... ..- attr(*, "comment")= chr "This variable follows normal distribution"
例2
y1<-sample(0:1,20,replace=TRUE) y2<-sample(LETTERS[1:5],20,replace=TRUE) df2<-data.frame(y1,y2) df2输出结果
y1 y2 1 0 B 2 1 E 3 0 B 4 1 A 5 1 D 6 0 A 7 0 E 8 1 D 9 0 D 10 0 D 11 0 E 12 0 C 13 1 A 14 1 D 15 0 B 16 0 E 17 1 C 18 0 C 19 1 D 20 1 C
str(df2)
'data.frame': 20 obs. of 2 variables: $ y1: int 0 1 0 1 1 0 0 1 0 0 ... $ y2: chr "B" "E" "B" "A" ...
在df2中添加变量的描述-
comment(df2$y1)<-c("This is a binary variable") comment(df2$y2)<-c("This is a categorical variable") str(df2)
'data.frame':20磅。2个变量-
$ y1: int 0 1 0 1 1 0 0 1 0 0 ... ..- attr(*, "comment")= chr "This is a binary variable" $ y2: chr "B" "E" "B" "A" ... ..- attr(*, "comment")= chr "This is a categorical variable"