Wrapping base R reshape for ease-of-use

后端 未结 4 1270
无人及你
无人及你 2020-12-16 15:16

It is a truth universally acknowledged that R\'s base reshape command is speedy and powerful but has miserable syntax. I have therefore written a quick wrapper around it wh

4条回答
  •  悲哀的现实
    2020-12-16 15:50

    Perhaps for those who are lazy and don't like to type the variable names, you can add the following to the head of your function:

      if (is.numeric(id) == 1) {
        id = colnames(data)[id]
      } else if (is.numeric(id) == 0) {
        id = id
      }
    
      if (is.numeric(vary) == 1) {
        vary = colnames(data)[vary]
      } else if (is.numeric(vary) == 0) {
        vary = vary
      }
    

    Then, following with your examples, you can use the following shorthand:

    reshapeasy(x.wide, direction="long", id=1, sep="_", vary="id")
    reshapeasy(x.long, direction="wide", id=6, vary=1)
    

    (I know, it might not be good practice since the code might be less readable or less easily understandable by someone later on, but it does happen frequently.)

提交回复
热议问题