Dear all, let suppose I have following vector:> dat1 <- c(rep("asd", 5), rep("xyz", 12), rep("erd", 17)) > dat1 <- dat1[sample(1:length(dat1), length(dat1), replace=F)] > dat1[1] "erd" "xyz" "erd" "asd" "asd" "erd" "xyz" "asd" "erd" "erd" "asd" "xyz" "erd" "asd" "xyz" "xyz" "erd" "xyz" "erd" [20] "erd" "erd" "xyz" "xyz" "erd" "erd" "erd" "erd" "xyz" "xyz" "xyz" "erd" "xyz" "erd" "erd" Here I want to know the length of replications for each unique items viz "asd", "xyz", and "erd". Is there any R function available to directly implement that? Thanks, [[alternative HTML version deleted]]
Barry Rowlingson
2010-Aug-20 10:31 UTC
[R] Determining the length of unique items in a vector
On Fri, Aug 20, 2010 at 11:01 AM, Ron Michael <ron_michael70 at yahoo.com> wrote:> Dear all, let suppose I have following vector: > >> dat1 <- c(rep("asd", 5), rep("xyz", 12), rep("erd", 17)) >> dat1 <- dat1[sample(1:length(dat1), length(dat1), replace=F)] >> dat1 > ?[1] "erd" "xyz" "erd" "asd" "asd" "erd" "xyz" "asd" "erd" "erd" "asd" "xyz" "erd" "asd" "xyz" "xyz" "erd" "xyz" "erd" > [20] "erd" "erd" "xyz" "xyz" "erd" "erd" "erd" "erd" "xyz" "xyz" "xyz" "erd" "xyz" "erd" "erd" > > > Here I want to know the length of replications for each unique items viz "asd", "xyz", and "erd". Is there any R function available to directly implement that?Like what rle does?>> rle(dat1)Run Length Encoding lengths: int [1:19] 1 3 2 3 1 1 1 2 1 3 ... values : chr [1:19] "asd" "erd" "asd" "erd" "xyz" "erd" "xyz" "erd" ... That's saying there's 1 asd, then 3 erd, then 2 asd, and so on. ?rle for more. Barry
Alain Guillet
2010-Aug-20 10:32 UTC
[R] Determining the length of unique items in a vector
Hi, You can try sapply(levels(as.factor(dat1)),nchar) Alain On 20-Aug-10 12:01, Ron Michael wrote:> Dear all, let suppose I have following vector: > >> dat1<- c(rep("asd", 5), rep("xyz", 12), rep("erd", 17)) >> dat1<- dat1[sample(1:length(dat1), length(dat1), replace=F)] >> dat1 > [1] "erd" "xyz" "erd" "asd" "asd" "erd" "xyz" "asd" "erd" "erd" "asd" "xyz" "erd" "asd" "xyz" "xyz" "erd" "xyz" "erd" > [20] "erd" "erd" "xyz" "xyz" "erd" "erd" "erd" "erd" "xyz" "xyz" "xyz" "erd" "xyz" "erd" "erd" > > > Here I want to know the length of replications for each unique items viz "asd", "xyz", and "erd". Is there any R function available to directly implement that? > > Thanks, > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help at 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.-- Alain Guillet Statistician and Computer Scientist SMCS - IMMAQ - Universit? catholique de Louvain Bureau c.316 Voie du Roman Pays, 20 B-1348 Louvain-la-Neuve Belgium tel: +32 10 47 30 50