Stefan Björk
2009-Jan-23 11:05 UTC
[R] Relative frequency of cases in data frame matching a specified criteria
I want to get the relative frequency of cases in a data frame that matches a specified criteria, omiting NA values. This seem so simple, but I can't come up with an effective way. nrow(data[data$variable>value & !is.na(data$variable),])/nrow(data) works but is very ineffective and CPU consuming when the data frame is large (more than 13000 rows and 400 columns). /S
Dimitris Rizopoulos
2009-Jan-23 11:20 UTC
[R] Relative frequency of cases in data frame matching a specified criteria
in this case you just need: mean(data$variable > value & !is.na(data$variable)) I hope it helps. Best, Dimitris Stefan Bj?rk wrote:> I want to get the relative frequency of cases in a data frame that > matches a specified criteria, omiting NA values. This seem so simple, > but I can't come up with an effective way. > > nrow(data[data$variable>value & !is.na(data$variable),])/nrow(data) > > works but is very ineffective and CPU consuming when the data frame is > large (more than 13000 rows and 400 columns). > > /S > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Possibly Parallel Threads
- Frequency and summary statistics table with different variables and categories
- Special Characters in artist field
- Why is there still ineffective code after -o3 optimization?
- umask 007 ineffective with newly created Maildir message
- Implement VLIW Backend on LLVM (Assembler Related Questions)