try this:
> x <- readLines(textConnection("---CLUSTER 1 ---
+ 3
+ 4
+ 5
+ 6
+ ---CLUSTER 2 ---
+ 9
+ 10
+ 8
+ 11"))>
> # create a list of where the 'clusters' are
> clust <- c(grep("CLUSTER", x), length(x) + 1L)
>
> # get size of each cluster
> clustSize <- diff(clust) - 1L
>
> # get cluster number
> clustNum <- gsub("[^0-9]+", "",
x[grep("CLUSTER", x)])
>
> result <- do.call(rbind, lapply(seq(length(clustNum)), function(.cl){
+ cbind(Object = x[seq(clust[.cl] + 1L, length = clustSize[.cl])]
+ , Cluster = .cl
+ )
+ }))>
> result
Object Cluster
[1,] "3" "1"
[2,] "4" "1"
[3,] "5" "1"
[4,] "6" "1"
[5,] "9" "2"
[6,] "10" "2"
[7,] "8" "2"
[8,] "11" "2">
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Wed, Apr 23, 2014 at 5:58 AM, Nico Met <nicomet80@gmail.com> wrote:
> 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.
>
[[alternative HTML version deleted]]