This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to filter genes in R

I have a list of gene names in file a.File a has 1 variable and 264 observations. Another list(file b) which includes several gene names and information about these genes, file b has 10 variables and 16558 observations. I do it in R and I want to check file b for the genes in file a and I want to extract whole row an put into an empty data frame(filec) when the gene name specified in file a matches with the gene name in file b. I write something like this:

filea <- read.csv("filea.csv", sep = ";")
fileb <-read.csv("fileb.csv", sep = ";")
filec <- data.frame()

for (i in 1:dim(filea)[1]) {
  for (j in 1:dim(fileb)[1]) {
    if (as.character(filea[i, 1]) == as.character(fileb[j, 1])) {   
     filec <- merge(fileb[j, 1], filec)
    }
  }
}

But it gives error. What can I do? Thanks in advance.

r

Err, why don't you drop the for loops? You'll also want to directly merge filea and fileb.

BTW, you might want to use the dplyr package, it provides more efficient join methods (e.g., left_join()).

2 answers

all you want is just:

filec <- fileb[fileb[,1] %in% filea[,1], ]

It helped, thank you for your answer :)

Seconded. Use the set you want as a data frame and the records as another data frame. Make sure the Column ids are set as V1 and then use plyr to join by 'V1' . Define this command as a new variable so you get the output as a data frame. Save as table using write.table function. No need for a loop. Just make sure you untick na strings and don't use quotes when inputting to R studio. Can cause problems with plyr joining.

Log in to answer this question.