Hi, You could try: Lines1 <- readLines(textConnection("---CLUSTER 1 --- 3 4 5 6 ---CLUSTER 2 --- 9 10 8 11")) indx <- grepl("CLUSTER", Lines1) res <- data.frame(Object=Lines1[!indx], Cluster= as.numeric(as.character(factor(cumsum(indx)[-which(indx)], labels=gsub("\\D+","",Lines1[indx])))))
res A.K. ----- Original Message ----- From: Nico Met <nicome...@gmail.com> To: R help <r-help@r-project.org> Cc: Sent: Wednesday, April 23, 2014 3:28 PM Subject: [R] Formatting with strings Dear all, I have object where I stored clusters in the following manner: ---CLUSTER 1 --- 3 4 5 6 ---CLUSTER 2 --- 9 10 8 11 Now want to format the data in the following way: Object Cluster 3 1 4 1 5 1 6 1 9 2 10 2 8 2 11 2 How can I do this in R? Thanks Nico [[alternative HTML version deleted]] ______________________________________________ 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. ______________________________________________ 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.