[R] identifying which column an observation comes from?
arun
smartpink111 at yahoo.com
Wed Oct 16 21:49:24 CEST 2013
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.
More information about the R-help
mailing list