Answers.Body: <p>The <code>merge</code> function of R might be useful. The function expects two data frames, with [optionally] columns to compare, and it will output all fields from both data frames where the comparison columns match.</p>
  
  <pre><code>&gt; a &lt;- data.frame(test=1:10);
  &gt; b &lt;- data.frame(test=c(sample(1:10,5),sample(c("A","B","C","D","E"))),
                    output=rnorm(10));
  &gt; merge(a, b, by="test");
    test     output
  1    1 -1.3800443
  2    2  0.2934274
  3    3  0.0868700
  4    9  1.1766914
  5   10  0.3793448
  </code></pre>
  
  <p>The <code>merge</code> function can be customised quite a bit (e.g. if you want NA set for non-matching rows), see <code>?merge</code> for more details.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-30T20:13:12.490Z
Answers.LastActivityDate: 2018-04-30T20:13:12.490Z
Answers.UserDisplayName: gringer
Answers.Body: <p>When you read in your two CSV files, you can explicitly declare the variable types for each column:</p>
  
  <pre><code>&gt; a &lt;- read.csv(..., colClasses=c("character"), ...)
  &gt; b &lt;- read.csv(..., colClasses=c("character", "numeric", "numeric", ...), ...)
  </code></pre>
  
  <p>You could also investigate <code>as.is</code> or <code>stringsAsFactors</code>, although this can cause problems where there are duplicate strings. Using <code>colClasses</code> is more explicit. </p>
  
  <p>Read the help documentation in <code>?read.csv</code> for a more complete explanation of these operators.</p>
  
  <p>You can filter with the <code>%in%</code> operator in <code>match</code>:</p>
  
  <pre><code>&gt; strings_of_interest &lt;- as.vector(a$V1)
  &gt; b.filtered &lt;- b[b$V1 %in% strings_of_interest,]
  </code></pre>
  
  <p>If I am reading your question correctly, perhaps you can then take <code>b.filtered</code> and turn that into the matrix you want.</p>
  
  <p>Avoid use of <code>for</code> loops where you can. They can slow R down considerably. A good read on this subject is in the third chapter of Patrick Burns' <a href="https://www.burns-stat.com/pages/Tutor/R_inferno.pdf" rel="nofollow noreferrer">The R Inferno</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-09T18:31:44.260Z
Answers.Text: Is there a reason for the downvoting?
Answers.UserDisplayName: Alex Reynolds
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-11T02:41:18.930Z
Answers.Text: I think it is bad for a community when downvoting without any explanation. Please explain yourself.
Answers.UserDisplayName: Alex Reynolds
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-30T19:00:10.517Z
Answers.LastActivityDate: 2018-04-30T19:05:50.140Z
Answers.LastEditDate: 2018-04-30T19:05:50.140Z
Answers.UserDisplayName: Alex Reynolds
Body: <p>I have two csv file - one is a list of strings and other has both strings and floating values. If the strings in both the files match I need to copy the complete row from the second file to a temporary array. I have declared a matrix with n rows and m columns and saved is as dataframe <code>temp &lt;- as.data.frame(matrix(nrow = n, ncol = m))</code>. I am using <code>for</code> loop to iterate over all rows and <code>strcmp</code> to compare the strings. But when the data is written to <code>temp</code> the strings are stored as integers. How should I declare this array? Is it possible to write this loop without declaring an array in R?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-30T17:39:20.657Z
Comments.Text: This question is unrelated to bioinformatics. You should ask somewhere more appropriate (e.g. stackoverflow). You might also want to add a minimal working example, it will be easier for other people to help you (see: https://stackoverflow.com/help/mcve). Good luck !
Comments.UserDisplayName: cmdoret
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-30T18:57:11.833Z
Comments.Text: I'm downvoting because this isn't really a bioinformatics question, but I suggest you take a look at `dplyr::semi_join`
Comments.UserDisplayName: heathobrien
Tags: <r>
Title: Declaring an array of strings and integers
ThreadText: # Declaring an array of strings and integers
  
  (Asked by: username_0 on 2018-04-30)
  
  I have two csv file - one is a list of strings and other has both strings and floating values. If the strings in both the files match I need to copy the complete row from the second file to a temporary array. I have declared a matrix with n rows and m columns and saved is as dataframe `temp <- as.data.frame(matrix(nrow = n, ncol = m))`. I am using `for` loop to iterate over all rows and `strcmp` to compare the strings. But when the data is written to `temp` the strings are stored as integers. How should I declare this array? Is it possible to write this loop without declaring an array in R?
  
  **Tags:** r
  
  > **Comments:**
  > **username_1**: This question is unrelated to bioinformatics. You should ask somewhere more appropriate (e.g. stackoverflow). You might also want to add a minimal working example, it will be easier for other people to help you (see: https://stackoverflow.com/help/mcve). Good luck !  
  > **username_2**: I'm downvoting because this isn't really a bioinformatics question, but I suggest you take a look at `dplyr::semi_join`  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2018-04-30. Score: 1 | Accepted answer
  
  The `merge` function of R might be useful. The function expects two data frames, with [optionally] columns to compare, and it will output all fields from both data frames where the comparison columns match.
  
  ```
  > a <- data.frame(test=1:10);
  > b <- data.frame(test=c(sample(1:10,5),sample(c("A","B","C","D","E"))),
                    output=rnorm(10));
  > merge(a, b, by="test");
    test     output
  1    1 -1.3800443
  2    2  0.2934274
  3    3  0.0868700
  4    9  1.1766914
  5   10  0.3793448
  
  ```
  
  The `merge` function can be customised quite a bit (e.g. if you want NA set for non-matching rows), see `?merge` for more details.
  
  
  ---
  
  ### Answer by username_4 on 2018-04-30. Score: 1
  
  When you read in your two CSV files, you can explicitly declare the variable types for each column:
  
  ```
  > a <- read.csv(..., colClasses=c("character"), ...)
  > b <- read.csv(..., colClasses=c("character", "numeric", "numeric", ...), ...)
  
  ```
  
  You could also investigate `as.is` or `stringsAsFactors`, although this can cause problems where there are duplicate strings. Using `colClasses` is more explicit.
  
  Read the help documentation in `?read.csv` for a more complete explanation of these operators.
  
  You can filter with the `%in%` operator in `match`:
  
  ```
  > strings_of_interest <- as.vector(a$V1)
  > b.filtered <- b[b$V1 %in% strings_of_interest,]
  
  ```
  
  If I am reading your question correctly, perhaps you can then take `b.filtered` and turn that into the matrix you want.
  
  Avoid use of `for` loops where you can. They can slow R down considerably. A good read on this subject is in the third chapter of Patrick Burns' [The R Inferno](https://www.burns-stat.com/pages/Tutor/R_inferno.pdf).
  
  > **Comments:**
  > **username_4**: Is there a reason for the downvoting?  
  > **username_4**: I think it is bad for a community when downvoting without any explanation. Please explain yourself.  
  
  ---
  
  