Hello !! I have this problem: A matrix on True/False and as many numerical vectors as columns, but of different length. What I 'd like to get is this: set.seed(12)> dat <- as.data.frame(matrix(as.logical(sample(T:F, 30, T)),5,6)) > colnames(dat) <- letters[1:6] > rownames(dat) <- paste(letters[1:5],1:5, sep="") > data b c d e f a1 TRUE TRUE TRUE TRUE TRUE TRUE b2 FALSE TRUE FALSE TRUE FALSE FALSE c3 FALSE FALSE TRUE FALSE TRUE TRUE d4 TRUE TRUE TRUE FALSE FALSE TRUE e5 TRUE TRUE TRUE TRUE TRUE FALSE> > v1 <- c(1:3) > names(v1) <- c("a1", "d4", "e5") > v2 <- c(1:4) > names(v2) <- c( "a1","b2", "c3","e5") >v1 a1 d4 e5 1 2 3> v2a1 b2 c3 e5 1 2 3 4>Desirable Output dat a b c d e f a1 1 1 ..... b2 NA 2 ..... c3 NA NA d4 2 3 e5 3 4 ...... Any suggestion , Thank you Anna Anna Freni Sterrantino Ph.D Student Department of Statistics University of Bologna, Italy via Belle Arti 41, 40124 BO. [[alternative HTML version deleted]]
If I understand your question, this may work for you: dat <- matrix(as.logical(sample(T:F, 30, T)),5,6) colnames(dat) <- letters[1:6] rownames(dat) <- paste(letters[1:5],1:5, sep="") dat1 <- matrix(NA,5,6) colnames(dat1) <- colnames(dat) rownames(dat1) <- rownames(dat) dat1[dat] <- unlist(sapply(apply(dat,2,sum),seq)) -- View this message in context: http://n4.nabble.com/Filling-a-logical-matrices-with-values-tp1469365p1470763.html Sent from the R help mailing list archive at Nabble.com.
Sorry, I noticed my previous code does not work if a column has all NAs. Try this instead: dat <- matrix(as.logical(sample(T:F, 30, T)),5,6) colnames(dat) <- letters[1:6] rownames(dat) <- paste(letters[1:5],1:5, sep="") dat1 <- matrix(NA,5,6) colnames(dat1) <- colnames(dat) rownames(dat1) <- rownames(dat) dat1[dat] <- unlist(sapply(apply(dat,2,sum), function(x) {if(x > 0) {seq(x)} else {list()}})) -- View this message in context: http://n4.nabble.com/Filling-a-logical-matrices-with-values-tp1469365p1470775.html Sent from the R help mailing list archive at Nabble.com.