<p>I know this is not really an answer, but I strongly discourage you from doing what you do, it's dangerous to impute data manually because it's prone to human mistakes - that you might mess up which columns should carry with metadata.</p>

<p>Actually, I think I have spotted a mistake already. This is your table using the corrected version of your code (with <code>metadata$Cytogenetics &lt;- NA</code>, see Devon's answer):</p>

<pre><code>&gt;metadata
        Group Cytogenetics
HSC1      HSC      Healthy
HSC2      HSC      Healthy
HSC3      HSC      Healthy
HSC4      HSC      Healthy
Blast11 Blast           NK
Blast12 Blast           NK
Blast1  Blast           NK
Blast2  Blast           NK
Blast3  Blast           NK
Blast4  Blast           NK
Blast6  Blast           NK
Blast7  Blast           NK
Blast8  Blast           NK
Blast9  Blast           NK
LSC1    Blast           NK
LSC2      LSC           NK
LSC3      LSC           NK
LSC4      LSC           NK
LSC6      LSC           NK
LSC7      LSC           NK
LSC8      LSC           NK
Blast5    LSC     Abnormal
LSC5      LSC     Abnormal
</code></pre>

<p>I sort of think that the column <code>LSC1</code> should be in the <code>LSC</code> group and visa versa the <code>Blast5</code> should be in <code>Blast</code> group. In this case it seems that it's derived from name, you can actually write a code that will extract the group from the name.</p>

<pre><code>df1 = read.csv("HSC_LSC_BLAST_karyotyope.txt", header = TRUE, sep = ",", row.names = 1)

metadata &lt;- data.frame(row.names = colnames(df1))
metadata$Group &lt;- sub("[^[:alpha:]]+", "", (colnames(df1)))
</code></pre>

<p>However, I don't know how do you know which sample is Healthy, NK or Abnormal. I suppose you have a table where this is written, if it's the case, use that table, don't impute the data manually.</p>

CC BY-SA 4.0
2019-02-22T13:10:14.463Z
yes actually the data im taking is realy big so im going for selected sample as you said to do it manually its prone to error ,one more question i want to add respective blood profiling data which are counts such as WBC and other counts, do i have to just make another data frame and add ,or i have to make a column of counts and add it as you shown?
kcm
CC BY-SA 4.0
2019-02-22T13:17:34.610Z
"I don't know how do you know which sample is Healthy, NK or Abnormal. I suppose you have a table where this is written" yes they have categorized based on karyotyping as well as other information
kcm
CC BY-SA 4.0
2019-02-22T13:55:00.783Z
This is not really the good platform for discussing good practices. I would just load the two tables and use function [merge](https://www.rdocumentation.org/packages/base/versions/3.5.2/topics/merge) to aggregate the data together.
Kamil S Jaron
CC BY-SA 4.0
2019-02-21T13:04:32.333Z
2019-02-22T09:58:02.773Z
2019-02-22T09:58:02.773Z
Kamil S Jaron
<p>Add the <code>Cytogenetics</code> columns and then assign things to a subset of it.</p>

<pre><code>metadata &lt;- data.frame(row.names = colnames(df1))
metadata<span class="math-container">$Group &lt;- NA     # you don't need to write a vector; just NA does the job
metadata$</span>Group[seq(1,4,1)] &lt;- 'HSC'
metadata<span class="math-container">$Group[seq(5,15,1)] &lt;- 'Blast'
metadata$</span>Group[seq(16,23,1)] &lt;- 'LSC'
metadata<span class="math-container">$Cytogenetics &lt;- NA    # initiate the column with NA values
metadata$</span>Cytogenetics[seq(1,4,1)] &lt;- 'Healthy'
metadata<span class="math-container">$Cytogenetics[seq(5,21,1)] &lt;- 'NK'
metadata$</span>Cytogenetics[seq(22,23,1)] &lt;- 'Abnormal'
</code></pre>

CC BY-SA 4.0
2019-02-20T20:37:27.400Z
2019-02-21T12:51:09.340Z
2019-02-21T12:51:09.340Z
Devon Ryan
<p>This is my <a href="https://drive.google.com/open?id=1BNfxuEuagZ0BSjMAN24cTIbIDxtBpBSg" rel="nofollow noreferrer">file</a></p>

<pre><code>df1 = read.csv("HSC_LSC_BLAST_karyotyope.txt",header = TRUE,sep = ",",row.names = 1)

metadata &lt;- data.frame(row.names = colnames(df1))
metadata<span class="math-container">$Group &lt;- rep(NA, ncol(df1))
metadata$</span>Group[seq(1,4,1)] &lt;- 'HSC'
metadata<span class="math-container">$Group[seq(5,15,1)] &lt;- 'Blast'
metadata$</span>Group[seq(16,23,1)] &lt;- 'LSC'
#metadata<span class="math-container">$Group[seq(13,16,1)] &lt;- 'Mono'
metadata$</span>Cytogenetics[seq(1,4,1)] &lt;- 'Healthy'
metadata<span class="math-container">$Cytogenetics[seq(5,21,1)] &lt;- 'NK'
metadata$</span>Cytogenetics[seq(22,23,1)] &lt;- 'Abnormal'
</code></pre>

<blockquote>
<pre><code>&gt;s metadata<span class="math-container">$Cytogenetics[seq(1,4,1)] &lt;- 'Healthy' Error in `$</span>&lt;-.data.frame`(`*tmp*`, Cytogenetics, value = c("Healthy",  :   
&gt; replacement has 4 rows, data has 23
</code></pre>
</blockquote>

<p>Im trying to add other metadata information based on clinical information apart from giving sample labeling , im certainly doing something wrong but im not sure what it is. As my idea is the another column named Cytogenetics which would have respective information status but im getting that error.</p>

<r>
Error in adding data in columns of R data frame
# Error in adding data in columns of R data frame

(Asked by: username_0 on 2019-02-20)

This is my file
df1 = read.csv("HSC_LSC_BLAST_karyotyope.txt",header = TRUE,sep = ",",row.names = 1)

metadata <- data.frame(row.names = colnames(df1))
metadata$Group <- rep(NA, ncol(df1))
metadata$Group[seq(1,4,1)] <- 'HSC'
metadata$Group[seq(5,15,1)] <- 'Blast'
metadata$Group[seq(16,23,1)] <- 'LSC'
#metadata$Group[seq(13,16,1)] <- 'Mono'
metadata$Cytogenetics[seq(1,4,1)] <- 'Healthy'
metadata$Cytogenetics[seq(5,21,1)] <- 'NK'
metadata$Cytogenetics[seq(22,23,1)] <- 'Abnormal'


>s metadata$Cytogenetics[seq(1,4,1)] <- 'Healthy' Error in `$<-.data.frame`(`*tmp*`, Cytogenetics, value = c("Healthy",  :   
> replacement has 4 rows, data has 23


Im trying to add other metadata information based on clinical information apart from giving sample labeling , im certainly doing something wrong but im not sure what it is. As my idea is the another column named Cytogenetics which would have respective information status but im getting that error.

**Tags:** r

---

## 2 answers

### Answer by username_1 on 2019-02-21. Score: 1 | Accepted answer

I know this is not really an answer, but I strongly discourage you from doing what you do, it's dangerous to impute data manually because it's prone to human mistakes - that you might mess up which columns should carry with metadata.
Actually, I think I have spotted a mistake already. This is your table using the corrected version of your code (with metadata$Cytogenetics <- NA, see Devon's answer):
>metadata
        Group Cytogenetics
HSC1      HSC      Healthy
HSC2      HSC      Healthy
HSC3      HSC      Healthy
HSC4      HSC      Healthy
Blast11 Blast           NK
Blast12 Blast           NK
Blast1  Blast           NK
Blast2  Blast           NK
Blast3  Blast           NK
Blast4  Blast           NK
Blast6  Blast           NK
Blast7  Blast           NK
Blast8  Blast           NK
Blast9  Blast           NK
LSC1    Blast           NK
LSC2      LSC           NK
LSC3      LSC           NK
LSC4      LSC           NK
LSC6      LSC           NK
LSC7      LSC           NK
LSC8      LSC           NK
Blast5    LSC     Abnormal
LSC5      LSC     Abnormal

I sort of think that the column LSC1 should be in the LSC group and visa versa the Blast5 should be in Blast group. In this case it seems that it's derived from name, you can actually write a code that will extract the group from the name.
df1 = read.csv("HSC_LSC_BLAST_karyotyope.txt", header = TRUE, sep = ",", row.names = 1)

metadata <- data.frame(row.names = colnames(df1))
metadata$Group <- sub("[^[:alpha:]]+", "", (colnames(df1)))

However, I don't know how do you know which sample is Healthy, NK or Abnormal. I suppose you have a table where this is written, if it's the case, use that table, don't impute the data manually.

> **Comments:**
> **username_0**: yes actually the data im taking is realy big so im going for selected sample as you said to do it manually its prone to error ,one more question i want to add respective blood profiling data which are counts such as WBC and other counts, do i have to just make another data frame and add ,or i have to make a column of counts and add it as you shown?  
> **username_0**: "I don't know how do you know which sample is Healthy, NK or Abnormal. I suppose you have a table where this is written" yes they have categorized based on karyotyping as well as other information  
> **username_1**: This is not really the good platform for discussing good practices. I would just load the two tables and use function [merge](https://www.rdocumentation.org/packages/base/versions/3.5.2/topics/merge) to aggregate the data together.  

---

### Answer by username_2 on 2019-02-20. Score: 2

Add the Cytogenetics columns and then assign things to a subset of it.
metadata <- data.frame(row.names = colnames(df1))
metadata$Group <- NA     # you don't need to write a vector; just NA does the job
metadata$Group[seq(1,4,1)] <- 'HSC'
metadata$Group[seq(5,15,1)] <- 'Blast'
metadata$Group[seq(16,23,1)] <- 'LSC'
metadata$Cytogenetics <- NA    # initiate the column with NA values
metadata$Cytogenetics[seq(1,4,1)] <- 'Healthy'
metadata$Cytogenetics[seq(5,21,1)] <- 'NK'
metadata$Cytogenetics[seq(22,23,1)] <- 'Abnormal'


---

