Skip to content

Variable labels

9 messages · Jim Lemon, PIKAL Petr, Steven Yen +3 more

#
Hallo Steven

You probably need to be more specific what is your intention. I still wonder what is the real problem you want to solve.

You loaded binary file and it resulted to 2 data frames. So far so good. But now I am lost.

You want to merge info from data frame "desc" to data frame "data"? You can use attr.
You want to make binary file which behaves like the one you get?  Use save/load.
You want to do something different? So please explain what exactly.

Cheers
Petr
#
Hi Steven,
I just happened to scan Petr's message to you and wondered if you were
looking for something related to the "describe" function in the
prettyR package (and a few others). For instance, if you do this:

library(prettyR)
describe(mtcars)

you get this:

Description of mtcars

Numeric
      mean median      var     sd valid.n
mpg   20.09  19.20    36.32   6.03      32
cyl    6.19   6.00     3.19   1.79      32
disp 230.72 196.30 15360.80 123.94      32
hp   146.69 123.00  4700.87  68.56      32
drat   3.60   3.70     0.29   0.53      32
wt     3.22   3.33     0.96   0.98      32
qsec  17.85  17.71     3.19   1.79      32
vs     0.44   0.00     0.25   0.50      32
am     0.41   0.00     0.25   0.50      32
gear   3.69   4.00     0.54   0.74      32
carb   2.81   2.00     2.61   1.62      32

However, you can call almost any summary function as an argument to
describe. Suppose I wrote a function "fackey" that produced this
output on a factor variable "city":

fackey(city)

label          numeric    count
New York   10            30
London       15            23
Paris          16            22
Rome         20            25

So if you ran "describe" on your data frame, you would get a list of
summary data frames that could be saved with the data frame in an
.Rdata file. Is this what you are looking for?

Jim
On Fri, May 14, 2021 at 4:59 PM PIKAL Petr <petr.pikal at precheza.cz> wrote:
#
Thanks to all, for bearing with me.

Now I realize expss may not be what I need. I have now written a 
self-runnable, replicable set of codes (listed below). Perhaps that 
gives an idea of what I need. Question is, whethet this is the right way 
to do this (to have a clickable object to learn about variable 
definitions) or whether there are better ways. Thanks!

Steven

rm(list=ls())
n<-6
mydata<-data.frame(id=1:n,
 ?????????????????? age=floor(rnorm(n,25,10)),
 ?????????????????? yrmarry=floor(rnorm(n,5,2)))
var.labels<-c(id? = "Individual ID",
 ????????????? age = "Age in Years",
 ????????????? yrmarry = "Years of marriage")
definitions<-as.data.frame(var.labels) # declare definitions as a data frame
save.image("c:/temp/a/try1.RData")???? # save binary .RData file
rm(list=ls())????????????????????????? # clean environment
load("c:/temp/a/try1.RData") # now load .RData file and definitions are 
clickable
 ???????????????????????????? # all I need is for user to be able to click
 ???????????????????????????? # and read the variable definitions
On 2021/5/14 ?? 05:15, Jim Lemon wrote:
#
Hm. What do you mean by "clickable".

#I can save any objects to a file
save(mydata,definitions, file="test.R") 
rm("mydata", "definitions")

#load them back
load("test.R")

#but it does not make them "clickable". Point and click is something I am familiar with in Excel or similar programs byt not in R.

#objects are back in the environment and one can inspect them by regular way (print, str, head, ...)
mydata
  id age yrmarry
1  1  35       4
2  2  31       6
3  3  21       4
4  4  20       3
5  5  19       7
6  6  24       5
definitions
               var.labels
id          Individual ID
age          Age in Years
yrmarry Years of marriage

If you want definitions to be part of the data file just use attr.

attr(mydata, "var.labels") <- definitions$var.labels

 attributes(mydata)
$names
[1] "id"      "age"     "yrmarry"

$class
[1] "data.frame"

$row.names
[1] 1 2 3 4 5 6

$var.labels
[1] "Individual ID"     "Age in Years"      "Years of marriage"

Cheers
Petr
#
Never mind what I said about "Clickable". All I meant was I created an 
item "definitions" that appears after I load the binary file, and that I 
can "click" (don's ask me what I mean by "click") the item in RStudio to 
read its contents -- variable definitions.

All I want to know at this pointis, is whether my way of getting the 
definitions in the environment "clumsy" and whether there are better 
ways to do it. Yes, you mention "attr..." but that is not as simple as 
viewing it in RStudio's environment pane. Thank you!
On 2021/5/14 ?? 06:37, PIKAL Petr wrote:
#
Well, that is the point. 

I do not use RStudio and dont know what it can or cannot do. So the save/load is probably everything you need if somebody with better knowledge of Rstudio does not come with better suggestion.

Cheers
Petr
#
On Fri, May 14, 2021 at 4:19 PM Steven Yen <styen at ntu.edu.tw> wrote:
I _think_ what you see as 'clickable' items are the names of variables
in your workspace. So if you need to make some attributes clickable,
you need to save them with a new name. E.g.,

f <- foreign::read.dta("https://sites.google.com/site/md4stata/linked/international-macroeconomic-data-set/ERS_USDA_Historical.dta")

f.attrs <- attributes(f)

'f.attrs' should now be visible and 'clickable'. But in theory, this
could also now contain more attributes that you cannot see in the
RStudio inspector, and you would have no way of knowing...

Hope that helps,

-Deepayan
#
You might also look into packages `Hmisc` and `labelVector` that provide
some support for labeling variable names.

David C
On Fri, May 14, 2021 at 1:59 AM PIKAL Petr <petr.pikal at precheza.cz> wrote:

            

  
  
#
On Fri, 14 May 2021 17:20:20 +0800
Steven Yen <styen at ntu.edu.tw> wrote: