[R] Variable labels
PIKAL Petr
petr@p|k@| @end|ng |rom prechez@@cz
Fri May 14 13:52:05 CEST 2021
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
> -----Original Message-----
> From: Steven Yen <styen using ntu.edu.tw>
> Sent: Friday, May 14, 2021 12:48 PM
> To: PIKAL Petr <petr.pikal using precheza.cz>
> Cc: R-help Mailing List <r-help using r-project.org>
> Subject: Re: [R] Variable labels
>
> 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:
> > 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
> >
> >> -----Original Message-----
> >> From: R-help <r-help-bounces using r-project.org> On Behalf Of Steven Yen
> >> Sent: Friday, May 14, 2021 11:20 AM
> >> To: Jim Lemon <drjimlemon using gmail.com>
> >> Cc: R-help Mailing List <r-help using r-project.org>
> >> Subject: Re: [R] Variable labels
> >>
> >> 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:
> >>> 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 using precheza.cz>
> >> wrote:
> >>>> 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
> >>>>
> >>>>
> >>>>> -----Original Message-----
> >>>>> From: Steven Yen <styen using ntu.edu.tw>
> >>>>> Sent: Thursday, May 13, 2021 5:53 PM
> >>>>> To: PIKAL Petr <petr.pikal using precheza.cz>
> >>>>> Subject: Re: [R] Variable labels
> >>>>>
> >>>>> Petr
> >>>>>
> >>>>> Those attachments (1.jpg, 2.jpg) I sent earlier were just screen
> >>>>> captures (with a third-party program) of what I saw in the
> >>>>> Environment pane right after loading the data. Sorry I cannot
> >>>>> explain my
> >> questions well enough.
> >>>>> All I was showing you was, right after loading the binary data
> >>>>> file, I saw two data frames---data which contain the data, and
> >>>>> desc which contains definitions of all variables (as shown in
> >>>>> 2.jpg). This is a data file from the publisher and I wanted to
> >>>>> know what it takes to create a binary data files along with
> >>>>> definitions of variables, both in the
> >> environment.
> >>>>> Steven
> >>>>>
> >>>>> On 2021/5/13 下午 09:51, PIKAL Petr wrote:
> >>>>>> Hi Steven
> >>>>>>
> >>>>>> I probably do not understand your question correctly. In 1 you
> >>>>>> show two
> >>>>> objects "data" 14x42 data frame and "desc" which is 2x14 data
> >>>>> frame, both residing in global environment.
> >>>>>> In 2 you show contents of data frame desc where variable are
> >>>>>> probably
> >>>>> variable names which are also in data object.
> >>>>>> names(data)
> >>>>>>
> >>>>>> and label which is some more elaborate description of the variable.
> >>>>>>
> >>>>>> If you want to move this label into your data object you probably
> >>>>>> could use attr
> >>>>>>
> >>>>>> attr(data, "label") <- desc$label
> >>>>>>
> >>>>>> If the order of "variable" is same as the order of data columns.
> >>>>>>
> >>>>>> I do not understand what do you mean by - how to get that "desc"
> >>>>>> in there in the environment? It is already part of global environment.
> >>>>>> You
> >>>>> want to create some new environment and move you desc there?
> >>>>>> Beside, your images are not familiar to me, this is plain R or
> >>>>>> some kind of
> >>>>> special GUI like R studio?
> >>>>>> Cheers
> >>>>>> Petr
> >>>>>>
> >>>>>>> -----Original Message-----
> >>>>>>> From: Steven Yen <styen using ntu.edu.tw>
> >>>>>>> Sent: Thursday, May 13, 2021 1:37 PM
> >>>>>>> To: PIKAL Petr <petr.pikal using precheza.cz>
> >>>>>>> Subject: Re: [R] Variable labels
> >>>>>>>
> >>>>>>> Petr
> >>>>>>>
> >>>>>>> Thanks. I am sending this to you privately as I am sending
> attachment.
> >>>>>>>
> >>>>>>> 1. I load the binary file and see the data frame and what
> >>>>>>> appears to be description (desc) alongside it (1.jpg).
> >>>>>>>
> >>>>>>> 2. Expanding "desc", I get to read the documentation (contents
> >>>>>>> of
> >> desc).
> >>>>>>> (2.jpg).
> >>>>>>>
> >>>>>>> #2 is all I need. I do not need to do anything fancy with the
> >>>>>>> variable label. I just like my students to have a simple ways of
> >>>>>>> learning the variables is the data file I provide to them.
> >>>>>>>
> >>>>>>> Again, my main question is, how to get that "desc" in there in
> >>>>>>> the environment. Thanks.
> >>>>>>>
> >>>>>>> Steven
> >>>>>>>
> >>>>>>> On 2021/5/13 下午 06:31, PIKAL Petr wrote:
> >>>>>>>> Hi.
> >>>>>>>>
> >>>>>>>> Maybe you could use attributes.
> >>>>>>>>
> >>>>>>>> dput(vec.m)
> >>>>>>>> structure(list(Group.1 = c(2003, 2021, 2003, 2021, 2003, 2021,
> >>>>>>>> 2003, 2021, 2003, 2021, 2003, 2021, 2003, 2021, 2003, 2021,
> >>>>>>>> 2003, 2021), variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L,
> >>>>>>>> 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L), .Label = c("s6",
> >>>>>>>> "s5", "s4", "s3", "s2", "s1.5", "s.7", "s.5", "pod"), class = "factor"),
> >>>>>>>> value = c(3.29, 0.525, 5.01, 1.385, 16.38, 7.67, 5.535, 3.28,
> >>>>>>>> 25.49, 24.41, 10.285, 12.79, 8.905, 12.92, 1.68, 3.67, 2.595,
> >>>>>>>> 5.06)), row.names = c(NA, -18L), class = "data.frame")
> >>>>>>>>
> >>>>>>>>> attr(vec.m, "some.kind.of.value") <- c("some specialvector",
> >>>>>>>>> "another special vector", "just ordinary vector")
> >>>>>>>> You can access them by attributes or attr.
> >>>>>>>>
> >>>>>>>> attributes(vec.m)
> >>>>>>>> $row.names
> >>>>>>>> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
> >>>>>>>>
> >>>>>>>> $names
> >>>>>>>> [1] "Group.1" "variable" "value"
> >>>>>>>>
> >>>>>>>> $class
> >>>>>>>> [1] "data.frame"
> >>>>>>>>
> >>>>>>>> $some.kind.of.value
> >>>>>>>> [1] "some specialvector" "another special vector" "just ordinary
> >>>>> vector"
> >>>>>>>>> attr(vec.m, "some")
> >>>>>>>> [1] "some specialvector" "another special vector" "just ordinary
> >>>>> vector"
> >>>>>>>> Cheers
> >>>>>>>> Petr
> >>>>>>>>
> >>>>>>>>> -----Original Message-----
> >>>>>>>>> From: R-help <r-help-bounces using r-project.org> On Behalf Of
> >>>>>>>>> Steven
> >>>>> Yen
> >>>>>>>>> Sent: Thursday, May 13, 2021 10:07 AM
> >>>>>>>>> To: Fredrik Karlsson <dargosch using gmail.com>
> >>>>>>>>> Cc: R-help Mailing List <r-help using r-project.org>
> >>>>>>>>> Subject: Re: [R] Variable labels
> >>>>>>>>>
> >>>>>>>>> Thanks. What I need “appears” simple. The .RData file is
> >>>>>>>>> provided by a third party (likely converted from a different
> >>>>>>>>> data format such as SAS in which variable labels (not value
> >>>>>>>>> labels) are common). When I load the binary file, in the
> >>>>>>>>> “environment” I see, as expected, a data frame showing how
> >>>>>>>>> many observations for how many variables. In addition, there
> >>>>>>>>> is also an item (in the
> >>>>>>>>> environment) (say “desc”) containing a list of variable labels
> >>>>>>>>> (definitions). I simply like to know how to get “desc” in the
> >>>>>>>>> environment—-it is a convenient way to show definitions of all
> >>>>>>>>> variables when you send a binary data file to a third party.
> >>>>>>>>> Thank
> >> you.
> >>>>>>>>>> On May 13, 2021, at 2:57 PM, Fredrik Karlsson
> >>>>>>>>>> <dargosch using gmail.com>
> >>>>>>>>> wrote:
> >>>>>>>>>> Hi,
> >>>>>>>>>>
> >>>>>>>>>> I am sorry but I don't understand your question, Generally,
> >>>>>>>>>> "clicking" is not
> >>>>>>>>> something you can assume to be implemented for anything in R.
> >>>>>>>>>> However, if you read the manual for the package
> >>>>>>>>>>
> >>>>>>>>>> https://gdemin.github.io/expss/
> >>>>>>>>>>
> >>>>>>>>>> you get an example at the bottom where an illustration of how
> >>>>>>>>>> the package
> >>>>>>>>> can be used to create Excel tables which would then be easy to
> >>>>>>>>> interact with through clicking.
> >>>>>>>>>> Is that what you wanted?
> >>>>>>>>>>
> >>>>>>>>>> Fredrik
> >>>>>>>>>>
> >>>>>>>>>>> On Thu, May 13, 2021 at 4:49 AM Steven Yen
> >> <styen using ntu.edu.tw>
> >>>>>>> wrote:
> >>>>>>>>>>> I insert variable with the expss function as shown below. No
> >>>>>>>>>>> error message. My question is, how to save the variable
> >>>>>>>>>>> labels in the data frame so that I can click to read the labels.
> Thank you.
> >>>>>>>>>>>
> >>>>>>>>>>> mydata<-read_excel("data/Excel/hseinv.xlsx",na=".")
> >>>>>>>>>>> library(expss)
> >>>>>>>>>>> mydata=apply_labels(mydata,
> >>>>>>>>>>> year ="1947-1988",
> >>>>>>>>>>> inv ="real housing inv, millions $",
> >>>>>>>>>>> pop ="population, 1000s",
> >>>>>>>>>>> price ="housing price index; 1982 = 1",
> >>>>>>>>>>> linv ="log(inv)",
> >>>>>>>>>>> lpop ="log(pop)",
> >>>>>>>>>>> lprice ="log(price)",
> >>>>>>>>>>> t ="time trend: t=1,...,42",
> >>>>>>>>>>> invpc ="per capita inv: inv/pop",
> >>>>>>>>>>> linvpc ="log(invpc)",
> >>>>>>>>>>> lprice_1="lprice[_n-1]",
> >>>>>>>>>>> linvpc_1="linvpc[_n-1]",
> >>>>>>>>>>> gprice ="lprice - lprice_1",
> >>>>>>>>>>> ginvpc ="linvpc - linvpc_1")
> >>>>>>>>>>>
> >>>>>>>>>>> ______________________________________________
> >>>>>>>>>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and
> >>>>>>>>>>> more, see https://stat.ethz.ch/mailman/listinfo/r-help
> >>>>>>>>>>> PLEASE do read the posting guide
> >>>>>>>>>>> http://www.R-project.org/posting-guide.html
> >>>>>>>>>>> and provide commented, minimal, self-contained,
> reproducible
> >>>>> code.
> >>>>>>>>>> --
> >>>>>>>>>> "Life is like a trumpet - if you don't put anything into it,
> >>>>>>>>>> you don't get
> >>>>>>>>> anything out of it."
> >>>>>>>>>
> >>>>>>>>> [[alternative HTML version deleted]]
> >>>>>>>>>
> >>>>>>>>> ______________________________________________
> >>>>>>>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more,
> >>>>>>>>> see https://stat.ethz.ch/mailman/listinfo/r-help
> >>>>>>>>> PLEASE do read the posting guide
> >>>>>>>>> http://www.R-project.org/posting- guide.html and provide
> >>>>>>>>> commented, minimal, self-contained, reproducible code.
> >>>> ______________________________________________
> >>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>>> PLEASE do read the posting guide
> >>>> http://www.R-project.org/posting-guide.html
> >>>> and provide commented, minimal, self-contained, reproducible code.
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide http://www.R-project.org/posting-
> >> guide.html and provide commented, minimal, self-contained,
> >> reproducible code.
More information about the R-help
mailing list