Hello, Maybe it's no longer needed but, another way would be the function below. It's more complicated because you don't need to know that there are only for categories. Only the original form and the wanted output.
fun <- function(x, var.to.transform){ f <- function(x, nm){ if(is.na(x[1])){ result <- rep(NA, length(nm)) }else{ result <- rep(0, length(nm)) names(result) <- nm tmp <- unlist(strsplit(x, split="-")) if(length(tmp) == 1){ result[tmp] <- 1 }else{ tmp <- matrix(tmp, nrow=2) result[tmp[1, ]] <- as.numeric(tmp[2, ]) } } result } if(is.character(var.to.transform)) inx.names.x <- which(var.to.transform == names(x)) else{ inx.names.x <- var.to.transform var.to.transform <- names(x)[inx.names.x] } X <- strsplit(as.character(x[, inx.names.x]), split = ";", fixed = TRUE) X.suf <- strsplit(unlist(X), split = "-", fixed = TRUE) X.suf <- sapply(X.suf, function(x) x[1]) X.suf <- unique(X.suf[!is.na(X.suf)]) X.names <- paste(var.to.transform, X.suf, sep="_") res <- x[, -inx.names.x] res[, X.names] <- t(sapply(X, f, X.suf)) res } tc <- textConnection(" var1 var2 var3_00 var3_01 var3_02 var3_04 1 A 1 0 0 0 2 B 0 1 3 1 3 C 0 2 1 0 4 D 0 0 0 12 5 E NA NA NA NA ") wanted <- read.table(tc, header=TRUE) close(tc) (res1 <- fun(x, "var3")) (res2 <- fun(x, 3)) all.equal(wanted, res1) all.equal(wanted, res2) Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Split-values-in-vector-tp4309651p4315357.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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.