subsetting problem

9 views
Skip to first unread message

Christopher Hamm

unread,
Oct 18, 2015, 6:41:19 PM10/18/15
to davi...@googlegroups.com
Ladies and gentlemen, 

I am having a subsetting issue that I think I have solved but would like someone to tell me I am wrong or right. I want to select only rows that have ALL 0’s for males and values greater than 0 for at least one female.

I have a data.matrix (cydno.rsem) with 1000 rows and 20 columns. The first 10 columns are males, and the last 10 columns are females. The data are integers ranging from 0 to ~1000. It think this pull out all the rows of males that are all 0’s:

cydno.rsem[rowSums(cydno.rsem != 0) == 0, 1:10]


Am I correct? 

Thanks

Chris

-- 
Christopher Hamm
Postdoctoral Researcher
Department of Ecology and Evolutionary Biology
University of Kansas
5032 Haworth Hall
1200 Sunnyside Avenue
Lawrence, Kansas 66045-7566
USA

email: ch...@ku.edu
phone: (785) 864-3848
@butterflyology

Noam Ross

unread,
Oct 18, 2015, 7:16:46 PM10/18/15
to davi...@googlegroups.com

Am I correct in that by “pull out”, you mean remove all the rows of males that are all zeros? I think this will not quite do this, because if you have non-zero females in the same row, that row will be left in. Rather, I think you want

cyndo.rsem[rowSums(cydno.resm[1:10, ]) == 0, 1:10)

--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Christopher Hamm

unread,
Oct 18, 2015, 7:25:40 PM10/18/15
to davi...@googlegroups.com
Thanks Noam, 

I was not clear. I wanted to make sure that at least one female had a value in a row that had all males with 0’s. 

Chris

Michael Hannon

unread,
Oct 18, 2015, 7:47:31 PM10/18/15
to davi...@googlegroups.com
You can test this on a fake data set. Please have a look at the
appended. Let me/us know if that does/does not help. The result I
get from running it:

> cydno.rsem[chooseRows, ]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0 0 0 0 1 1
[2,] 0 0 0 0 0 1
>

-- Mike


rows <- 12
cols <- 6

set.seed(628)

cydno.rsem <- matrix(sample(c(0,1), rows * cols,
prob=c(0.6, 0.4), replace=TRUE), nrow=rows)
cydno.rsem

males <- 1:(cols / 2)
females <- (cols / 2 + 1):cols
males
females

chooseRows <- apply(cydno.rsem, 1, function(nextRow) {
all(nextRow[males] == 0) &
!all(nextRow[females] == 0)
})
cydno.rsem[chooseRows, ]

Christopher Hamm

unread,
Oct 18, 2015, 8:06:35 PM10/18/15
to davi...@googlegroups.com
Hi all, 

Here is the solution I went with (courtesy of Dr. Ross):

cydno.rsem[rowSums(cydno.rsem[, 1:10]) == 0 & rowSums(cydno.rsem[, 11:20]) !=0, ]


Thanks

CH
Reply all
Reply to author
Forward
0 new messages