0

How I can change the order of the levels of a factor in a Label Attribute? I want to implement this R command:

(Label<- relevel(Label, ref = "Yes")

How can I do that ?

dpel
  • 1,954
  • 1
  • 21
  • 31
  • 1
    Welcome to Stack Overflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 May 05 '17 at 19:21
  • Maybe this post helps? http://stackoverflow.com/questions/18413756/re-ordering-factor-levels-in-data-frame – zx8754 May 05 '17 at 19:21

1 Answers1

1

You could use the Execute R operator. Here's an example that changes the levels of the label in the iris data set. If you view the RapidMiner log you can see the print statements that show the structures.

<?xml version="1.0" encoding="UTF-8"?><process version="7.2.003">
  <context>
    <input/>
    <output/>
    <macros/>
  </context>
  <operator activated="true" class="process" compatibility="7.2.003" expanded="true" name="Process">
    <process expanded="true">
      <operator activated="true" class="retrieve" compatibility="7.2.003" expanded="true" height="68" name="Retrieve Iris" width="90" x="112" y="85">
    <parameter key="repository_entry" value="//Samples/data/Iris"/>
      </operator>
      <operator activated="true" class="r_scripting:execute_r" compatibility="7.2.000" expanded="true" height="103" name="Execute R" width="90" x="246" y="85">
    <parameter key="script" value="# rm_main is a mandatory function, &#10;# the number of arguments has to be the number of input ports (can be none)&#10;rm_main = function(data)&#10;{&#10;    print('Hello, world!')&#10;    # output can be found in Log View&#10;    &#10;    # your code goes here&#10;&#10;&#9;data$label = factor(data$label)&#10;&#9;data2 = data&#10;&#9;data2$label = relevel(data2$label, ref = 'Iris-virginica')&#10;&#9;print(str(data))&#10;&#9;print(str(data2))&#10;&#9;&#10;    return(list(data, data2))&#10;}&#10;"/>
      </operator>
      <connect from_op="Retrieve Iris" from_port="output" to_op="Execute R" to_port="input 1"/>
      <connect from_op="Execute R" from_port="output 1" to_port="result 1"/>
      <connect from_op="Execute R" from_port="output 2" to_port="result 2"/>
      <portSpacing port="source_input 1" spacing="0"/>
      <portSpacing port="sink_result 1" spacing="0"/>
      <portSpacing port="sink_result 2" spacing="0"/>
      <portSpacing port="sink_result 3" spacing="0"/>
    </process>
  </operator>
</process>

I observe that the id and label roles are removed after the R script but this can be fixed by using the Set Role operator.

Andrew Chisholm
  • 6,362
  • 2
  • 22
  • 41