Keep rows with columns having values between -1 and 1 from a dataframe in R
2
1
Entering edit mode
3.0 years ago

I have a dataframe in R that looks like this:

   V1         T1         T2         T3         T4        T5
  CXCL6  0.8536601  1.0903336  3.7633042  5.5800459 5.8477150
  PPBP  0.7739450  0.3587961  0.5073359  0.2743522 0.6221722
  CXCL10  0.1258370 -0.3535165 -0.7460387  3.5604672 0.1971432
  CXCL11 -0.2563139  0.7117200  0.0000000 -0.2288303 0.9955557
  CXCL12  0.6181279  1.7529310  1.7637760  1.2752787 1.2284810

I want to keep the rows that have values only between -1 and 1.

I have tried this command but unfortunately it does not work.

condition1 <- Genes[,c(2:6)] <=-1 & Genes[,c(2:6)] >=1
Genes <- Genes[condition1,]

Can someone tell me where I am wrong so that I can successfully filter my dataframe.

R • 2.0k views
ADD COMMENT
1
Entering edit mode

Assuming these are log2 fold changes or something like that, I don't know why you would want columns with values between -1 and 1, because most people want values exactly outside of that range. I may be reading this wrong, but you also seem to be asking for values <=-1 which is to the "left" of -1, and >=1 which is to the "right" of 1.

ADD REPLY
1
Entering edit mode

Did you try && instead of & ?

ADD REPLY
1
Entering edit mode

Since the length(condition1) is >1, and && check only for the first element in vector, && should be avoided here.

ADD REPLY
2
Entering edit mode
3.0 years ago
Basti ★ 2.0k

Your condition is not clear, but if you want rows that contain values that are ONLY between -1 and 1, this should work :

Genes[which(rowSums(  (Genes[,2:6]>=-1) & (Genes[,2:6]<=1))==5),]
ADD COMMENT
0
Entering edit mode

Thank you for your comment but unfotunately it did not work for me.

ADD REPLY
0
Entering edit mode

What do you mean by it did not work? Do you get an error or it doesn't do what you expect?

ADD REPLY
0
Entering edit mode

didn't do what i expected unfortunately.

ADD REPLY
2
Entering edit mode
3.0 years ago

There should be more tidy way for that, but here is my solution.

rowIdx = as.numeric()
for(i in 1:nrow(Genes)){
  cond = Genes[-1][i,] >= -1 & Genes[-1][i,] <=1
  if(all(cond == TRUE)){
    rowIdx = c(rowIdx, i)
  }
}

Then subset your dataframe by rowIdx:

GenesSubset = Genes[rowIdx,]
ADD COMMENT

Login before adding your answer.

Traffic: 1799 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