ooh.. I have another question. What if I want to add the value in the vector a to the hello each time it prints. Here is your output
a <- c(2,3,5,5,5,6,6,7) mapply(rep, "hello", rle(a)$lengths, USE.NAMES = FALSE) [[1]] [1] "hello" [[2]] [1] "hello" [[3]] [1] "hello" "hello" "hello" [[4]] [1] "hello" "hello" [[5]] [1] "hello" If I want something like this, based on values in vector a. [[1]] [1] "hello 2" [[2]] [1] "hello 3" [[3]] [1] "hello 5" "hello 5" "hello 5" [[4]] [1] "hello 6" "hello 6" [[5]] [1] "hello 7" What i am actually doing is hmm.. I have a bunch of text files which is output from another program. I want to extract some specific information from these files and write to a new file and save it. All these files have a certain variable k which maybe 2, or 3 or 5 etc. The vector a shows the k values of 8 of such files. I want the contents of all files with k value 5 to be written into one file. Thanks, Roy On Mon, Jan 24, 2011 at 11:43 PM, Erik Iverson <er...@ccbr.umn.edu> wrote: > > > Roy Mathew wrote: > >> Thanks for the reply Erik, As you mentioned, grouping consecutive elements >> of 'a' was my idea. I am unaware of any R'ish way to do it. It would be nice >> if someone in the community knows this. >> > > Is this the idea you're trying to execute? It uses ?rle and ?mapply. > > > a <- c(2,3,5,5,5,6,6,7) > mapply(rep, "hello", rle(a)$lengths, USE.NAMES = FALSE) > > [[1]] > [1] "hello" > > [[2]] > [1] "hello" > > [[3]] > [1] "hello" "hello" "hello" > > [[4]] > [1] "hello" "hello" > > [[5]] > [1] "hello" > -- Best Regards, Roy [[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.