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?

  • $\begingroup$ 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: stackoverflow.com/help/mcve). Good luck ! $\endgroup$
    – cmdoret
    Apr 30 '18 at 17:39
  • $\begingroup$ I'm downvoting because this isn't really a bioinformatics question, but I suggest you take a look at dplyr::semi_join $\endgroup$ Apr 30 '18 at 18:57

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"))),
> 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.


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.

  • $\begingroup$ Is there a reason for the downvoting? $\endgroup$ Jan 9 '19 at 18:31
  • $\begingroup$ I think it is bad for a community when downvoting without any explanation. Please explain yourself. $\endgroup$ Jan 11 '19 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.