[R] identifying which column an observation comes from?
arun
smartpink111 at yahoo.com
Wed Oct 16 22:47:21 CEST 2013
In cases like these:
mat <- matrix(data = c(1,0,0,0,1,0,0,0,1,1,1,0), nrow = 3, byrow = T)
rownames(mat) <- c("AL", "MS", "FL")
rowSums(col(mat) * mat)
#AL MS FL
# 1 1 6
A.K.
On Wednesday, October 16, 2013 4:18 PM, Bert Gunter <gunter.berton at gene.com> wrote:
?col
rowSums(col(mat) * mat)
produces a named vector that you can convert to a data frame if you like, although it's really not necessary.
Cheers,
Bert
On Wed, Oct 16, 2013 at 12:49 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,
>Try:
>
>ind <- which(mat==1,arr.ind=TRUE)[,2]
>dat1<- data.frame(Code=names(ind),ind=ind,stringsAsFactors=FALSE)
> row.names(dat1) <- 1:nrow(dat1)
>A.K.
>
>
>
>
>
>On Wednesday, October 16, 2013 3:29 PM, Karl Fetter <karl.fetter at gmail.com> wrote:
>Hello,
>
>I have a matrix of samples (rows) and haplotypes (columns), where 0
>indicates that a sample does not posses that columns haplotype and 1
>indicates it does. So sample1 has 0's for every column, except the column
>that represents haplotype X, and it has a 1.
>
>
>I want a length(sample) x 2 dataframe that tells me which samples have what
>haplotype.
>
>I need some way of identifying for every row, where is the 1? And then
>print that information in a new column so I can see what row and which
>haplotype.
>
>Here is an example of what I mean:
>
>mat <- matrix(data = c(1,0,0,0,1,0,0,0,1), nrow = 3, byrow = T)
>
>rownames(mat) <- c("AL", "MS", "FL")
>
>> mat
>
> [,1] [,2] [,3]
>
>AL 1 0 0
>
>MS 0 1 0
>
>FL 0 0 1
>
>
>How can I make a data frame like this:
>
>AL 1
>
>MS 2
>
>FL 3
>
>
>Thanks for your ideas and time,
>
>Karl
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
--
Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374
More information about the R-help
mailing list