[Fwd: [R] Create a vector of combinations based on a table column names]
Adaikalavan Ramasamy
ramasamy at cancer.org.uk
Tue Nov 23 17:38:09 CET 2004
x <- c(1,2,3.095,1,1,0,0,0,0,0,0,
2,2,1.687,1,0,1,0,0,0,0,0,
46,3,3.47,1,0,0,0,1,0,0,1,
47,3,1.563,1,0,0,0,0,1,1,0,
50,3,6.234,0,1,1,1,0,0,0,0,
148,4,3.663,0,0,1,0,1,0,1,1,
151,4,3.47,0,0,0,1,1,1,0,1,
177,5,5.411,1,0,1,1,1,0,0,1,
178,5,6.829,1,0,1,1,0,1,1,0)
mat <- matrix(x, nc=11, byrow=T); rm(x)
colnames(mat) <- c("index", "no", "value", LETTERS[1:8])
apply( mat[ , 4:11], 1, function(x) paste( names(x)[ which(x==1) ],
collapse=" " ))
[1] "A B" "A C" "A E H" "A F G" "B C D" "C E G H"
[7] "D E F H" "A C D E H" "A C D F G"
mat[ , 4:11] uses data from 4th to 11 th columns.
names(x)[ which(x==1) ] finds the names of all elements with values 1 in
the rows
paste( ..., collapse=" ") collapses all the names of the elements with
value 1
apply( mat[ , 4:9], 1, ... ) applies the above process for every row
Regards, Adai
-----Forwarded Message-----
From: Henrik Andersson <h.andersson at nioo.knaw.nl>
To: r-help at stat.math.ethz.ch
Subject: [R] Create a vector of combinations based on a table column names
Date: Tue, 23 Nov 2004 16:40:59 +0100
I want to create a character vector based on the table (shortened for
display) below:
Where there are ones in the matrix I want the column name to appear and
where there are zeros nothing, which would make the vector in this
shortened case:
combinations <- ("A B","A C","A E H","A F G","B C D","E G H",A C D E
H","A C D F G")
no value A B C D E F G H
1 2 3.095 1 1 0 0 0 0 0 0
2 2 1.687 1 0 1 0 0 0 0 0
46 3 3.470 1 0 0 0 1 0 0 1
47 3 1.563 1 0 0 0 0 1 1 0
50 3 6.234 0 1 1 1 0 0 0 0
148 4 3.663 0 0 1 0 1 0 1 1
151 4 3.470 0 0 0 1 1 1 0 1
177 5 5.411 1 0 1 1 1 0 0 1
178 5 6.829 1 0 1 1 0 1 1 0
Question is how to make this not so manually?
---------------------------------------------
Henrik Andersson
Netherlands Institute of Ecology -
Centre for Estuarine and Marine Ecology
P.O. Box 140
4400 AC Yerseke
Phone: +31 113 577473
h.andersson at nioo.knaw.nl
http://www.nioo.knaw.nl/ppages/handersson
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
--
Adaikalavan Ramasamy ramasamy at cancer.org.uk
Centre for Statistics in Medicine http://www.ihs.ox.ac.uk/csm/
Cancer Research UK Tel : 01865 226 677
Old Road Campus, Headington, Oxford Fax : 01865 226 962
More information about the R-help
mailing list