This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Keep rows with columns having values between -1 and 1 from a dataframe in R

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

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.

Did you try && instead of & ?

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

2 answers

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),]

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

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

didn't do what i expected unfortunately.

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,]

Log in to answer this question.