Re: [R] Counting various elemnts in a vactor
Hi, library(plyr) df1<-count(df) rep(df1[,1],df1[,2]*100) count(as.character(rep(df1[,1],df1[,2]*100))) # x freq #1 A 200 #2 B 200 #3 C 200 #4 D 400 #5 F 400 A.K. - Original Message - From: Katherine Gobin To: r-help@r-project.org Cc: Sent: Tuesday, March 26, 2013 4:12 AM Subject: [R] Counting various elemnts in a vactor Dear R forum I have a vector say as given below df = c("F", "C", "F", "B", "D", "A", "D", "D", "A", "F", "D", "F", "B", "C") I need to find (1) how many times each element occurs? e.g. in above vector F occurs 4 times, C occurs 2 times etc. (2) Depending on the number of occurrences, I need to repeat the element 100 times of the occurrences e.g. I need to repeat F 6 * 100 = 600 times, C 2*100 = 200 times. I can manage the second part i.e. repeating but I am not able to count the number of times the element is appearing in a given vector. Kindly guide Katherine [[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.
Re: [R] Counting various elemnts in a vactor
Dear Sir, Thanks a lot for your great help. I couldn't have figured it out. Thanks again. Regards Katherine --- On Tue, 26/3/13, D. Rizopoulos wrote: From: D. Rizopoulos Subject: Re: [R] Counting various elemnts in a vactor To: "Katherine Gobin" Cc: "r-help@r-project.org" Date: Tuesday, 26 March, 2013, 8:23 AM try this: df <- c("F", "C", "F", "B", "D", "A", "D", "D", "A", "F", "D", "F", "B", "C") tab <- table(df) tab rep(names(tab), 100 * tab) I hope it helps. Best, Dimitris On 3/26/2013 9:12 AM, Katherine Gobin wrote: > Dear R forum > > I have a vector say as given below > > df = c("F", "C", "F", "B", "D", "A", "D", "D", "A", "F", "D", "F", "B", > "C") > > I need to find > > (1) how many times each element occurs? e.g. in above vector F occurs 4 > times, C occurs 2 times etc. > > (2) Depending on the number of occurrences, I need to repeat the element 100 > times of the occurrences e.g. I need to repeat F 6 * 100 = 600 times, C 2*100 > = 200 times. > > I can manage the second part i.e. repeating but I am not able to count the > number of times the element is appearing in a given vector. > > Kindly guide > > Katherine > > > > > > > > > > > > [[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. > -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ [[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.
Re: [R] Counting various elemnts in a vactor
try this: df <- c("F", "C", "F", "B", "D", "A", "D", "D", "A", "F", "D", "F", "B", "C") tab <- table(df) tab rep(names(tab), 100 * tab) I hope it helps. Best, Dimitris On 3/26/2013 9:12 AM, Katherine Gobin wrote: > Dear R forum > > I have a vector say as given below > > df = c("F", "C", "F", "B", "D", "A", "D", "D", "A", "F", "D", "F", "B", > "C") > > I need to find > > (1) how many times each element occurs? e.g. in above vector F occurs 4 > times, C occurs 2 times etc. > > (2) Depending on the number of occurrences, I need to repeat the element 100 > times of the occurrences e.g. I need to repeat F 6 * 100 = 600 times, C 2*100 > = 200 times. > > I can manage the second part i.e. repeating but I am not able to count the > number of times the element is appearing in a given vector. > > Kindly guide > > Katherine > > > > > > > > > > > > [[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. > -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ 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] Counting various elemnts in a vactor
Dear R forum I have a vector say as given below df = c("F", "C", "F", "B", "D", "A", "D", "D", "A", "F", "D", "F", "B", "C") I need to find (1) how many times each element occurs? e.g. in above vector F occurs 4 times, C occurs 2 times etc. (2) Depending on the number of occurrences, I need to repeat the element 100 times of the occurrences e.g. I need to repeat F 6 * 100 = 600 times, C 2*100 = 200 times. I can manage the second part i.e. repeating but I am not able to count the number of times the element is appearing in a given vector. Kindly guide Katherine [[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.