Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are numerical values stored as factors in R?
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/14244147/r-interpreting-variable-in-data-frame-as-factor-it-isnt">R interpreting variable in data frame as factor; it isn’t</a> </p> </blockquote> <p>I am having an issue with some code I've written in R and I think my problems are stemming from a poor understanding of factors in R.</p> <p><strong>What I am trying to do:</strong> I have a column numbers and I want to create another column of bin descriptions (e.g. 1,2,3, etc which correspond to bin1, bin2, etc.)</p> <p>Here is the code:</p> <pre><code>## import data to data frame rawData = read.table("Data.csv", sep = ",", header = TRUE) ## reduce socre into bins rawData["s10_BIN"] = NA for(i in 1:length(rawData[,"foo"])) { if(rawData[i,"s10"] &gt; 0 &amp;&amp; rawData[i,"s10"] &lt;= .5) { rawData[i,"s10_BIN"] = 1 } else if(rawData[i,"s10"] &gt; .5 &amp;&amp; rawData[i,"s10"] &lt;= 1) { rawdata[i,"s10_BIN"] = 2 } else if(rawData[i,"s10"] &gt; 1 &amp;&amp; rawData[i,"s10"] &lt;= 1.5) { rawData[i,"s10_BIN"] = 3 } else if(rawData[i,"s10"] &gt; 1.5 &amp;&amp; rawData[i,"s10"] &lt;= 2) { rawData[i,"s10_BIN_BIN"] = 4 } else rawData[i,"s10_BIN"] = 0 } </code></pre> <p>I keep getting the error</p> <pre><code>1: In Ops.factor(rawData[i, "s10"], 0) : &gt; not meaningful for factors 2: In Ops.factor(rawData[i, "s10"], 0.5) : &lt;= not meaningful for factors </code></pre> <p>Can someone give me insight as to what I am missing? Thanks!</p> <p>EDIT: Here are the first 2 rows of the data:</p> <pre><code> rawData[1:2,] ID PRODUCT GROUP MARKET s10 CLAIMS NUM_CLAIMS s10_BIN 1 1 foo A C NULL -5 2 NA 2 2 foo A D 1.064 1 1 NA </code></pre> <p>I have a feeling the issue may be the NULL values...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload