Selecting specific rows in R
1
0
Entering edit mode
4.4 years ago
azadsaei • 0

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 • 3.2k views
ADD COMMENT
2
Entering edit mode
4.4 years ago
ATpoint 82k

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"),])
ADD COMMENT
1
Entering edit mode

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" ,]
ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 2749 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6