-6

Let's say I have a factorial variable x.fine which is too fine for analysis (too many levels), and I want to reduce the number of levels by creating a reference table translating x.fine to a new variable x.coarse (which I haven't developed yet).

I could open up Excel or OpenRefine and start creating the lookup values interactively, export the table as CSV and import for a join in R. Since I have to create the table only once and it will be included in the analysis, reproducibility is guaranteed.

But is there a simple way to create such a reference table without leaving R/RStudio?

EDIT: Changed title (replaced "interactive" with "user-friendly")

Timm S.
  • 5,135
  • 6
  • 24
  • 38
  • With "simple" I mean a way that helps me speed up the process, e.g. by autocompletion of already used x.coarse values, or helping me code the reference values by listing the x.fine values with assignment signs, etc. A way that removes unnecessary typing, copying, deleting. – Timm S. Jan 29 '16 at 16:15
  • 1
    The method [shown here](http://stackoverflow.com/a/9604339/2572423) looks pretty good. – JasonAizkalns Jan 29 '16 at 16:15
  • 1
    @arumbay autocompletion mostly depends on your text editor, it has little to do with R... – scoa Jan 29 '16 at 22:04

2 Answers2

2

Maybe library(hash)?

library(hash)
x.fine <- c("a", "b", "c", "d", "e")
h <- hash(a = "X", b = "X", c = "Y", d = "Y", e = "Z")
x.coarse <- values(h[x.fine])
Kota Mori
  • 6,510
  • 1
  • 21
  • 25
2

The function questionr::irec opens a shiny application with the variable original values and fields to input the new values. It copies the full code to the console on exit.

questionr::irec(mydf,"x.fine")
scoa
  • 19,359
  • 5
  • 65
  • 80