Hi, This is what I'm trying to do: 1. I have a vector split.indexes <- c(1, 3, 3, 6, 6, 6) 2. I have another vector with something like data <- c(6, 4, 4, 5, 3, 2) 3. I use groups <- tapply(data, split.indexes, "c") to create a list of vectors based on the levels in split.indexes. As a result groups has the following values: $`1` [1] 6 $`3` [1] 4 4 $`6` [1] 5 3 2 This is the desired result. 4. The next thing I want to do is call paste on groups so I can get a string representation that I can write to a file. However, when I call paste(groups) it returns the following: [1] "6" "c(4, 4)" "c(5, 3, 2)" I do not understand why the paste function is adding the "c()" part. I would like the result to be: [1] "6" "4 4" "5 3 2" Do you know any efficient way to get rid of the "c()"? Note: I'm working with a very large data set 1-10 millions of points and I would rather not use for loops since the performance are really bad. Sincerely, Nikola [[alternative HTML version deleted]]
HI, You could either get the results by: unlist(lapply(groups,function(x) paste(x,collapse=" ")),use.names=FALSE) #[1] "6"???? "4 4"?? "5 3 2" #or gsub("[c(),]","",paste(groups)) #[1] "6"???? "4 4"?? "5 3 2" str(gsub("[c(),]","",paste(groups))) # chr [1:3] "6" "4 4" "5 3 2" ?str(unlist(lapply(groups,function(x) paste(x,collapse=" ")),use.names=FALSE)) # chr [1:3] "6" "4 4" "5 3 2" A.K. ----- Original Message ----- From: Nikola Janevski <njanevsk at gmail.com> To: r-help at r-project.org Cc: Sent: Thursday, February 14, 2013 8:28 PM Subject: [R] Using paste on results from tapply? Hi, This is what I'm trying to do: 1. I have a vector split.indexes <- c(1, 3, 3, 6, 6, 6) 2. I have another vector with something like data <- c(6, 4, 4, 5, 3, 2) 3. I use groups <- tapply(data, split.indexes, "c") to create a list of vectors based on the levels in split.indexes. As a result groups has the following values: $`1` [1] 6 $`3` [1] 4 4 $`6` [1] 5 3 2 This is the desired result. 4. The next thing I want to do is call paste on groups so I can get a string representation that I can write to a file. However, when I call paste(groups) it returns the following: [1] "6"? ? ? ? ? "c(4, 4)"? ? "c(5, 3, 2)" I do not understand why the paste function is adding the "c()" part. I would like the result to be: [1] "6"? ? ? ? ? "4 4"? ? "5 3 2" Do you know any efficient way to get rid of the "c()"? Note: I'm working with a very large data set 1-10 millions of points and I would rather not use for loops since the performance are really bad. Sincerely, Nikola ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
HI, You could also use these: sapply(groups,paste,collapse=" ") #????? 1?????? 3?????? 6 ?# ? "6"?? "4 4" "5 3 2" #or gsub("^ | $","",gsub("\\D+"," ",paste(groups))) #[1] "6"???? "4 4"?? "5 3 2" A.K. ----- Original Message ----- From: Nikola Janevski <njanevsk at gmail.com> To: r-help at r-project.org Cc: Sent: Thursday, February 14, 2013 8:28 PM Subject: [R] Using paste on results from tapply? Hi, This is what I'm trying to do: 1. I have a vector split.indexes <- c(1, 3, 3, 6, 6, 6) 2. I have another vector with something like data <- c(6, 4, 4, 5, 3, 2) 3. I use groups <- tapply(data, split.indexes, "c") to create a list of vectors based on the levels in split.indexes. As a result groups has the following values: $`1` [1] 6 $`3` [1] 4 4 $`6` [1] 5 3 2 This is the desired result. 4. The next thing I want to do is call paste on groups so I can get a string representation that I can write to a file. However, when I call paste(groups) it returns the following: [1] "6"? ? ? ? ? "c(4, 4)"? ? "c(5, 3, 2)" I do not understand why the paste function is adding the "c()" part. I would like the result to be: [1] "6"? ? ? ? ? "4 4"? ? "5 3 2" Do you know any efficient way to get rid of the "c()"? Note: I'm working with a very large data set 1-10 millions of points and I would rather not use for loops since the performance are really bad. Sincerely, Nikola ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.