<p>The following code would create a new column with the desired values, 0 values will have <code>NA</code> in this new column.</p>

<pre><code>custom.cn.data[custom.cn.data$CN &lt; 0, "CN_string"] &lt;- "Del" 
custom.cn.data[custom.cn.data$CN &gt; 0, "CN_string"] &lt;- "Amp"
</code></pre>

CC BY-SA 4.0
2020-01-29T10:48:09.770Z
2020-01-29T10:48:09.770Z
DavyCats
<p>Try to stay within one package (paradigm) for one problem. This will make the code more readable, easier to debug.</p>

<p>At the moment you have data as a <em>data.table</em> object from <strong>data.table</strong> package, then trying to add a new column using <em>mutate</em> from <strong>dplyr</strong> package, and within <em>mutate</em> using <strong>base</strong> <em>replace</em> function.</p>

<p>Solutions:</p>

<p><strong>data.table</strong></p>

<pre><code># update by row index
custom.cn.data[ CN &lt; 0, NewColumn := "Del" ]
custom.cn.data[ CN &gt;= 0, NewColumn := "Amp" ]
</code></pre>

<p>or use <em>ifelse</em></p>

<pre><code>custom.cn.data[, NewColumn := ifelse(CN &lt; 0, "Del", "Amp") ]
</code></pre>

<p><strong>base</strong> </p>

<p>see <a href="https://bioinformatics.stackexchange.com/a/11264/131">@DavyCat's</a> and <a href="https://bioinformatics.stackexchange.com/a/11263/131">@haci's</a> answers.</p>

<p><strong>dplyr</strong></p>

<p>Note, this will change the <em>data.table</em> into <em>data.frame</em>.</p>

<pre><code># ifelse
custom.cn.data &lt;- custom.cn.data %&gt;%
  mutate(NewColumn = ifelse(CN &lt; 0, "Del", "Amp"))
</code></pre>

<p>or use <em>case_when</em></p>

<pre><code>custom.cn.data &lt;- custom.cn.data %&gt;%
  mutate(NewColumn = case_when(
    CN &lt; 0 ~ "Del",
    CN &gt;= 0 ~ "Amp"))
</code></pre>

CC BY-SA 4.0
2020-01-30T11:15:32.617Z
2020-01-30T11:15:32.617Z
zx8754
<pre><code>custom.cn.data<span class="math-container">$new_column &lt;- ifelse(custom.cn.data$</span>CN &lt; 0, "Del", "Amp")
</code></pre>

<p>In this scenario <code>CN == 0</code> would be <code>Amp</code></p>

CC BY-SA 4.0
2020-01-29T10:42:12.237Z
2020-01-29T10:42:12.237Z
haci
<p>I have a data frame</p>

<pre><code>&gt; head(custom.cn.data)
      Gene       Sample_name         CN
1: DDX11L1          F02         0.6788844
2: DDX11L1          C02        -0.9924524
3: DDX11L1          A07        -2.3833559
&gt; 
</code></pre>

<p>I want to convert values in CN column &lt; 0 to Del and >0 to Amp</p>

<p>But I am getting error</p>

<pre><code>&gt; a %&gt;% mutate(q = replace(CN, which(CN&lt;0), Del))
Error in replace(CN, which(CN &lt; 0), Del) : object 'Del' not found

&gt; a<span class="math-container">$CN &lt;- replace(a$</span>CN, which(a<span class="math-container">$CN &lt; 0), Del)
Error in replace(a$</span>CN, which(a$CN &lt; 0), Del) : object 'Del' not found
</code></pre>

<p>Any help?</p>

CC BY-SA 4.0
2020-01-29T20:14:50.920Z
You are getting these errors because you did not use quotes before and after `Del` and `R` looks for an object called `Del` instead of using the string "Del".
haci
CC BY-SA 4.0
2020-01-30T09:32:24.707Z
What happens to when CN is zero?
zx8754
<r>
Converting values to string
# Converting values to string

(Asked by: username_0 on 2020-01-29)

I have a data frame
> head(custom.cn.data)
      Gene       Sample_name         CN
1: DDX11L1          F02         0.6788844
2: DDX11L1          C02        -0.9924524
3: DDX11L1          A07        -2.3833559
> 

I want to convert values in CN column < 0 to Del and >0 to Amp
But I am getting error
> a %>% mutate(q = replace(CN, which(CN<0), Del))
Error in replace(CN, which(CN < 0), Del) : object 'Del' not found

> a$CN <- replace(a$CN, which(a$CN < 0), Del)
Error in replace(a$CN, which(a$CN < 0), Del) : object 'Del' not found

Any help?

**Tags:** r
> **Comments:**
> **username_1**: You are getting these errors because you did not use quotes before and after `Del` and `R` looks for an object called `Del` instead of using the string "Del".  
> **username_2**: What happens to when CN is zero?  

---

## 3 answers

### Answer by username_1 on 2020-01-29. Score: 6 | Accepted answer

custom.cn.data$new_column <- ifelse(custom.cn.data$CN < 0, "Del", "Amp")

In this scenario CN == 0 would be Amp


---

### Answer by username_3 on 2020-01-29. Score: 4

The following code would create a new column with the desired values, 0 values will have NA in this new column.
custom.cn.data[custom.cn.data$CN < 0, "CN_string"] <- "Del" 
custom.cn.data[custom.cn.data$CN > 0, "CN_string"] <- "Amp"


---

### Answer by username_2 on 2020-01-30. Score: 2

Try to stay within one package (paradigm) for one problem. This will make the code more readable, easier to debug.
At the moment you have data as a data.table object from data.table package, then trying to add a new column using mutate from dplyr package, and within mutate using base replace function.
Solutions:
data.table
# update by row index
custom.cn.data[ CN < 0, NewColumn := "Del" ]
custom.cn.data[ CN >= 0, NewColumn := "Amp" ]

or use ifelse
custom.cn.data[, NewColumn := ifelse(CN < 0, "Del", "Amp") ]

base 
see @DavyCat's and @haci's answers.
dplyr
Note, this will change the data.table into data.frame.
# ifelse
custom.cn.data <- custom.cn.data %>%
  mutate(NewColumn = ifelse(CN < 0, "Del", "Amp"))

or use case_when
custom.cn.data <- custom.cn.data %>%
  mutate(NewColumn = case_when(
    CN < 0 ~ "Del",
    CN >= 0 ~ "Amp"))


---

