text: user2247: 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?
  
  user776: 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.
  
  user73: 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.
  
original_text: 2247: <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>
   776: <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>
   73: <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>
  