How to add a variable description in R?

R ProgrammingServer Side ProgrammingProgramming

To add a variable description in R, we can use comment function and if we want to have a look at the description then structure call of the data frame will be used. For example, if we have a data frame say df that contains a column x then we can describe x by using the command comment(df$x)<-c("The name of this variable is x"). Now to have a look at it, we can use str(df). Example1 Consider the below data frame − Live Demo x1<-rnorm(20,21,3.24) x2<-rnorm(20,5,2.1) df1<-data.frame(x1,x2) df1 Output  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 ... Adding description of variables in df1 − comment(df1$x1)<-c("This variable follows normal distribution")
comment(df1$x2)<-c("This variable follows normal distribution") str(df1) 'data.frame': 20 obs. of 2 variables − $ 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" Example2 Live Demo y1<-sample(0:1,20,replace=TRUE) y2<-sample(LETTERS[1:5],20,replace=TRUE) df2<-data.frame(y1,y2) df2 Output  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" ... Adding description of variables in df2 − comment(df2$y1)<-c("This is a binary variable")
comment(df2$y2)<-c("This is a categorical variable") str(df2) 'data.frame': 20 obs. of 2 variables − $ 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"
Updated on 06-Mar-2021 12:16:47