This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Selecting specific rows in R

Hi, I have the following file. I want to have specific rows (eg.2 and 3) and remove rest of the rows which doesnt have any data (X). Is there a way you could help me to write the code in R for this?

Here is how my data looks

r

1 answer

That is a basic R question and you really should try to solve those yourself. But anyway, here is a solution. Try to understand what it is doing in order to do these things yourself in the future. Data filtering is a key skill in bioinformatics.

## example data:
df1 <- data.frame(X2=c("X",1,2,"X"),X3=c("X",3,4,"X"))

## detect "X", and omit those:
df1.cleaned <- na.omit(df1[(df1 != "X"),])

The other solution is;

## example data:
df1 <- data.frame(X2=c("X",1,2,"X"),X3=c("X",3,4,"X"))

## Delete "X" in each column separately
df1.cleaned <- df1[df1$X2 != "X" & df1$X3 != "X" ,]

Yeah, but this is not generic. Imagine a df with 1000 columns, you are not going to type that command for 1000 columns for sure.

I did not say this is the best solution. This is the another way of solving the current solution. Imagine you want to keep just rows with "X", you can easily modify the code and keep those rows.

Log in to answer this question.