Re: [R] counts and percentage of multiple categorical columns in R

2014-01-16 Thread arun
Hi Jingxia, May be this helps: dat1 <- read.table(text="fatfreemilk fatmilk halfmilk 2fatmilk A A A A A B B A B A A A C C C C D . A A A E A E C A B A A . A A A B . A A A B E",sep="",header=TRUE,stringsAsFactors=FALSE,check.names=FALSE,na.strings=".")  dat2 <- dat1  dat2$id <- 1:nrow(dat2) librar

Re: [R] counts and percentage of multiple categorical columns in R

2014-01-16 Thread arun
Also, You can do the same with the previous solution: result1 <- result[,-6] vec1 <- unique(unlist(dat1)) result2 <- as.data.frame(t(sapply(dat1,function(x) {counts<- table(factor(x,levels=vec1)); percentage<-sprintf("%.1f",(counts/sum(counts))*100); c(paste0(counts,paste0("(",percentage,")")),

Re: [R] counts and percentage of multiple categorical columns in R

2013-12-29 Thread arun
Hi, Another way is: vec1 <- unique(unlist(dat1))  res3 <- as.data.frame(t(sapply(dat1,function(x) {counts<- table(factor(x,levels=vec1)); percentage<-(counts/sum(counts))*100; paste0(counts,paste0("(",percentage,")"))})))   colnames(res3) <- vec1    identical(res3,as.data.frame(res2)) #[1] TRUE A

Re: [R] counts and percentage of multiple categorical columns in R

2013-12-29 Thread arun
Hi, Try: dat1 <- read.table(text="fatfreemilk fatmilk halfmilk 2fatmilk A A A A A B B A B A A A C C C C D A A A A E A E C A B A A A A A A B B A A A B E",sep="",header=TRUE,stringsAsFactors=FALSE,check.names=FALSE)  dat2 <- dat1  dat2$id <- 1:nrow(dat2) library(reshape2)  res <- dcast(melt(dat2,id.v

Re: [R] counts and percentage of multiple categorical columns in R

2013-12-29 Thread Bert Gunter
Is this homework? (We generally don't do homework here). However, hint: ?table and links therein. Also, as you can see below, post in plain text, not HTML, which is stripped and can lead to hard-to-read gobbledygook. Cheers, Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data

[R] counts and percentage of multiple categorical columns in R

2013-12-29 Thread Jingxia Lin
Dear R helpers, I have a data sheet (“milk”) with four types of milk from five brands (A, B, C, D, E), the column shows the brands that each customer chose for each type of the milk they bought. The data sheet goes like below. You can see for some type of milk, no brand is chosen. fatfreemilk fat

Re: [R] Counts of duplicate rows as a new column without grouping of duplicates raws.

2013-12-26 Thread arun
Also, you could try: library(plyr) ddply(dat1,.(A),mutate, D=length(A)) #or library(data.table)  dt1 <- data.table(dat1,key='A')  dt1[,D:=.N,by='A'] A.K. On , arun wrote: Hi, Try: dat1 <- read.table(text="A    B    C 1 a    1    ff 2 b    1    re 3 c    1    sd 5 a    2    as 6 c    4    fe 7

Re: [R] Counts of duplicate rows as a new column without grouping of duplicates raws.

2013-12-26 Thread arun
Hi, Try: dat1 <- read.table(text="A    B    C 1 a    1    ff 2 b    1    re 3 c    1    sd 5 a    2    as 6 c    4    fe 7 d    5  tt 8 d    5  tt 9 d    9  oi",sep="",header=TRUE,stringsAsFactors=FALSE)  within(dat1,D<-ave(seq_along(A),A,FUN=length)) A.K. On Thursday, December 26,

[R] Counts of duplicate rows as a new column without grouping of duplicates raws.

2013-12-26 Thread Bartłomiej Taciak
Hi everyone, My data looks like this one: AB C 1 a 1 ff 2 b 1 re 3 c 1 sd 5 a 2 as 6 c 4 fe 7 d 5 tt 8 d 5 tt 9 d 9 oi I want to add a new column D, which will contain how many an element from the column A is repeated,

Re: [R] counts on bars on barplot

2012-05-15 Thread arkanion
got it, did it, worked ! thanks a lot ! -- View this message in context: http://r.789695.n4.nabble.com/counts-on-bars-on-barplot-tp4630060p4630114.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https:

Re: [R] counts on bars on barplot

2012-05-15 Thread arkanion
I want to label each bar with the values of each bar (counts) on top of each of them. -- View this message in context: http://r.789695.n4.nabble.com/counts-on-bars-on-barplot-tp4630060p4630107.html Sent from the R help mailing list archive at Nabble.com. _

Re: [R] counts on bars on barplot

2012-05-15 Thread pannigh
If you use simple barplot graphic try this, e.g.: a<-barplot(some.data) text(a, some.data, some.data) The "text" command needs x and y coordinates, by assigning your barplot to "a" (here), you have the x coordinats and by using the y-values of the barplot, the values will be written ontop of the

Re: [R] counts on bars on barplot

2012-05-15 Thread John Kane
:34 -0700 (PDT) > To: r-help@r-project.org > Subject: [R] counts on bars on barplot > > How can I have the corresponding counts shown on each bar? > > -- > View this message in context: > http://r.789695.n4.nabble.com/counts-on-bars-on-barplot-tp4630060.html > Sent fro

[R] counts on bars on barplot

2012-05-15 Thread arkanion
How can I have the corresponding counts shown on each bar? -- View this message in context: http://r.789695.n4.nabble.com/counts-on-bars-on-barplot-tp4630060.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing

Re: [R] counts

2012-02-22 Thread Rui Barradas
Hello, Just look at your second 'apply': the 'function(x)' is not using the 'x' (!) Solution: function(x) hist(x, breaks=0:nrow(test), plot=FALSE)$counts) Note that instead of 'nrow' you could also use 'length(x)'. Hope this helps, Rui Barradas -- View this message in context: http://r.789

Re: [R] counts in quantiles in and from a matrix

2011-10-06 Thread Ben qant
Excellent! Thank you! ben On Wed, Oct 5, 2011 at 9:18 PM, Dennis Murphy wrote: > Hi: > > Here's one way: > > m <- matrix(rpois(100, 8), nrow = 5) > f <- function(x) { >q <- quantile(x, c(0.1, 0.9), na.rm = TRUE) >c(sum(x < q[1]), sum(x > q[2])) >} > > t(apply(m, 1, f)) > > HTH, > De

Re: [R] counts in quantiles in and from a matrix

2011-10-05 Thread Dennis Murphy
Hi: Here's one way: m <- matrix(rpois(100, 8), nrow = 5) f <- function(x) { q <- quantile(x, c(0.1, 0.9), na.rm = TRUE) c(sum(x < q[1]), sum(x > q[2])) } t(apply(m, 1, f)) HTH, Dennis On Wed, Oct 5, 2011 at 8:11 PM, Ben qant wrote: > Hello, > > I'm trying to get the count of value

[R] counts in quantiles in and from a matrix

2011-10-05 Thread Ben qant
Hello, I'm trying to get the count of values in each row that are above and below quantile thresholds. Thanks! Example: > x = matrix(1:30,5,6) > x [,1] [,2] [,3] [,4] [,5] [,6] [1,]16 11 16 21 26 [2,]27 12 17 22 27 [3,]38 13 18 23 28 [4,]4

Re: [R] counts of a vector

2010-05-26 Thread speretti
Thank you all! Particulary Wu Gong whose answer is very compact -- View this message in context: http://r.789695.n4.nabble.com/counts-of-a-vector-tp2232047p2232353.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org m

Re: [R] counts of a vector

2010-05-26 Thread Wu Gong
### Special cumsum ### Do cumsum when TRUE, and reset to 0 when FALSE x <- c( TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE ) ## The rle function computes the lengths of equal values of a vector ## And gives two vecters: $lengths and $values ## The sequence function creates and concaten

Re: [R] counts of a vector

2010-05-26 Thread William Dunlap
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of speretti > Sent: Wednesday, May 26, 2010 10:22 AM > To: r-help@r-project.org > Subject: [R]

Re: [R] counts of a vector

2010-05-26 Thread Gabor Grothendieck
Check out this thread for numerous solutions: https://stat.ethz.ch/pipermail/r-help/2007-June/134557.html On Wed, May 26, 2010 at 1:22 PM, speretti wrote: > > Hi, > > I need help to find an efficient way to transform a vector like: > > a<-c(1,1,0,1,0,0,0,1,1,1,1,0,1,0,1,1) > > in a vector that co

Re: [R] counts of a vector

2010-05-26 Thread Erik Iverson
speretti wrote: Hi, I need help to find an efficient way to transform a vector like: a<-c(1,1,0,1,0,0,0,1,1,1,1,0,1,0,1,1) in a vector that counts only di 1 elements, like: b<-c(1,2,0,1,0,0,0,1,2,3,4,0,1,0,1,2) Thank you! One way: rl <- rle(a) unlist(mapply("*", lapply(rl$lengths, fun

[R] counts of a vector

2010-05-26 Thread speretti
Hi, I need help to find an efficient way to transform a vector like: a<-c(1,1,0,1,0,0,0,1,1,1,1,0,1,0,1,1) in a vector that counts only di 1 elements, like: b<-c(1,2,0,1,0,0,0,1,2,3,4,0,1,0,1,2) Thank you! -- View this message in context: http://r.789695.n4.nabble.com/counts-of-a-vector-tp

Re: [R] counts of elements in vector

2010-03-29 Thread yehengxin
Try using aggregate( ) -- View this message in context: http://n4.nabble.com/counts-of-elements-in-vector-tp1704036p1710369.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailma

Re: [R] counts of elements in vector

2010-03-29 Thread David Winsemius
On Mar 29, 2010, at 6:52 PM, Ali Tofigh wrote: Assume you have a vector of characters x: x [1] "a" "b" "a" "d" "d" "c" I use a function that counts the number of times each string occurs in x: sapply(unique(x), function(s) {sum(x == s)}) a b d c 2 1 2 1 Is there a more efficient way

[R] counts of elements in vector

2010-03-29 Thread Ali Tofigh
Assume you have a vector of characters x: > x [1] "a" "b" "a" "d" "d" "c" I use a function that counts the number of times each string occurs in x: > sapply(unique(x), function(s) {sum(x == s)}) a b d c 2 1 2 1 Is there a more efficient way of doing this? Cheers, /Ali

Re: [R] counts of each column that are not NA, and/or greater than column means

2008-02-04 Thread Patrick Burns
Gabor Csardi wrote: >On Mon, Feb 04, 2008 at 03:21:10PM +0800, Ng Stanley wrote: > > >>Hi, >> >>Given a test matrix, test <- matrix(c(1,2,3,NA,2,3,NA,NA,2), 3,3) >> >>A) How to compute the counts of each column (excluding the NA) i.e., 3, 2, 1 >> >> > >apply(test, 2, function(x) sum(!is.na(x

Re: [R] counts of each column that are not NA, and/or greater than column means

2008-02-04 Thread Dimitris Rizopoulos
hits=-2.5 tests�YES_00,FORGED_RCVD_HELO X-USF-Spam-Flag: NO check the following options: test <- matrix(c(1,2,3,NA,2,3,NA,NA,2), 3, 3) # A colSums(!is.na(test)) # B mat <- test > rep(colMeans(test, na.rm = TRUE), each = nrow(test)) colSums(!is.na(mat) & mat) apply(test, 2, function(x) { m

Re: [R] counts of each column that are not NA, and/or greater than column means

2008-02-04 Thread Gabor Csardi
On Mon, Feb 04, 2008 at 03:21:10PM +0800, Ng Stanley wrote: > Hi, > > Given a test matrix, test <- matrix(c(1,2,3,NA,2,3,NA,NA,2), 3,3) > > A) How to compute the counts of each column (excluding the NA) i.e., 3, 2, 1 apply(test, 2, function(x) sum(!is.na(x))) > B) How to compute the counts of e

[R] counts of each column that are not NA, and/or greater than column means

2008-02-03 Thread Ng Stanley
Hi, Given a test matrix, test <- matrix(c(1,2,3,NA,2,3,NA,NA,2), 3,3) A) How to compute the counts of each column (excluding the NA) i.e., 3, 2, 1 B) How to compute the counts of each column (excluding the NA) that are greater than the column means ? i.e., 1, 1, 0 I could write a for loop, but