[Rd] reverse object creation
Gabriel Becker
gmbecker at ucdavis.edu
Fri Oct 9 17:06:27 CEST 2015
Bo,
More philosophically, the dput output is more idiomatically appropriate for
R.
Data.frames are emphatically column-major data structures in R, by (very
good) design.
Best,
~G
On Oct 9, 2015 3:55 AM, "Thierry Onkelinx" <thierry.onkelinx at inbo.be> wrote:
> Dear Bo,
>
> Please keep the mailing list in cc.
>
> Your function only works properly with a data.frame in which all variables
> are characters. dput() will preserve the structure of the object and works
> with all R objects.
>
> Best regards,
>
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
> Forest
> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> Kliniekstraat 25
> 1070 Anderlecht
> Belgium
>
> To call in the statistician after the experiment is done may be no more
> than asking him to perform a post-mortem examination: he may be able to say
> what the experiment died of. ~ Sir Ronald Aylmer Fisher
> The plural of anecdote is not data. ~ Roger Brinner
> The combination of some data and an aching desire for an answer does not
> ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
> 2015-10-09 12:32 GMT+02:00 Bo Werth <bo.werth at gmail.com>:
>
> > Dear Thierry,
> >
> > many thanks for the hint - indeed, I wasn't aware of dput() - it is doing
> > something close to what I was looking for -
> > the only issue with the result for data frames is that it becomes
> > difficult to see the row association:
> >
> > dput returns
> >
> > structure(list(sheet = c("output cup", "output coppy", "gross value added
> > cup",
> > "gross value added coppy", "GFCF cup", "GFCF vol", "empl jobs",
> > "empl FTE", "employees jobs", "employees FTE", "D1", "D11", "D29-D39",
> > "D29 nom", "D39 nom", "P51c nom", "B2n+B3n", "total hours worked",
> > "hours worked employees"), var = c("PROD", "PKPY", "VALU", "VKPY",
> > "GFCF", "GKPY", "EMPN", "FTEN", "EMPE", "FTEE", "LABR", "WAGE",
> > "OTXS", "D29 nom", "D39 nom", "CFCC", "NOPS", "HRNS", "HRSE")), .Names =
> > c("sheet",
> > "var"), row.names = c(NA, -19L), class = "data.frame")
> > >
> >
> > df_rev returns
> >
> > rbind.data.frame(c("output cup", "PROD"),
> > c("output coppy", "PKPY"),
> > c("gross value added cup", "VALU"),
> > c("gross value added coppy", "VKPY"),
> > c("GFCF cup", "GFCF"),
> > c("GFCF vol", "GKPY"),
> > c("empl jobs", "EMPN"),
> > c("empl FTE", "FTEN"),
> > c("employees jobs", "EMPE"),
> > c("employees FTE", "FTEE"),
> > c("D1", "LABR"),
> > c("D11", "WAGE"),
> > c("D29-D39", "OTXS"),
> > c("D29 nom", "D29 nom"),
> > c("D39 nom", "D39 nom"),
> > c("P51c nom", "CFCC"),
> > c("B2n+B3n", "NOPS"),
> > c("total hours worked", "HRNS"),
> > c("hours worked employees", "HRSE"))
> >
> > Thanks again,
> > best,
> > Bo
> >
> > On Fri, Oct 9, 2015 at 12:22 PM, Thierry Onkelinx <
> > thierry.onkelinx at inbo.be> wrote:
> >
> >> Dear Bo,
> >>
> >> I think that you are looking for dput()
> >>
> >> Best regards,
> >>
> >> ir. Thierry Onkelinx
> >> Instituut voor natuur- en bosonderzoek / Research Institute for Nature
> >> and Forest
> >> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> >> Kliniekstraat 25
> >> 1070 Anderlecht
> >> Belgium
> >>
> >> To call in the statistician after the experiment is done may be no more
> >> than asking him to perform a post-mortem examination: he may be able to
> say
> >> what the experiment died of. ~ Sir Ronald Aylmer Fisher
> >> The plural of anecdote is not data. ~ Roger Brinner
> >> The combination of some data and an aching desire for an answer does not
> >> ensure that a reasonable answer can be extracted from a given body of
> data.
> >> ~ John Tukey
> >>
> >> 2015-10-09 11:55 GMT+02:00 Bo Werth <bo.werth at gmail.com>:
> >>
> >>> Dear all,
> >>>
> >>> this is my first message to this mailing list - please advise if it is
> >>> not
> >>> the right place for the subject
> >>>
> >>> I've been using R very intensively the last 3-4 years and one of the
> most
> >>> tedious tasks is modification of lookup or conversion tables
> >>>
> >>> So far, I have not found functions that create the commands for
> creating
> >>> objects (vectors, data frames) based on the objects themselves - i.e.
> to
> >>> reverse-engineer them.
> >>>
> >>> Here are my suggestions:
> >>>
> >>> c_rev <- function(x) cat(paste0('c("', gsub(', ', '", "', toString(x)),
> >>> '")\n'))
> >>>
> >>> df_rev <- function(x) {
> >>> X <- apply(x, 1,
> >>> function(x) {
> >>> paste0('c(',
> >>> paste0(shQuote(x), collapse = ", "),
> >>> ')'
> >>> )
> >>> }
> >>> )
> >>> command <- paste0(
> >>> 'rbind.data.frame(',
> >>> paste0(
> >>> X,
> >>> collapse = ",\n"),
> >>> ')')
> >>> return(cat(command, "\n"))
> >>> }
> >>>
> >>> Bo
> >>>
> >>> ---
> >>> bowerth.github.io
> >>>
> >>> [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> R-devel at r-project.org mailing list
> >>> https://stat.ethz.ch/mailman/listinfo/r-devel
> >>>
> >>
> >>
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
[[alternative HTML version deleted]]
More information about the R-devel
mailing list