Hi,
how to do quickly equivalent of the following?
counter = 0
for(i in 1:length(data$S2)) {
if(!is.na(data$S2[i])) {
counter = counter + 1
}
}
I have imagined something like length(x,na.rm=TRUE).
How can I get values usually taken from tables like z-score,
values of t distribution etc.? I could not find them among
values mentioned in info file.
Thanks,
Matej
--
Matej Cepl, matej at ceplovi.cz, PGP ID# D96484AC
138 Highland Ave. #10, Somerville, Ma 02143, (617) 623-1488
The difference between death and taxes is death doesn't get worse
every time Congress meets
-- Will Rogers
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, Oct 23, 2002 at 02:48:20AM -0400, Matej Cepl wrote:> Hi, > > how to do quickly equivalent of the following? > > counter = 0 > for(i in 1:length(data$S2)) { > if(!is.na(data$S2[i])) { > counter = counter + 1 > } > } >Try: sum(is.na(data$2)) Hopin' it helps, L. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 23 Oct 2002, Matej Cepl wrote:> Hi, > > how to do quickly equivalent of the following? > > counter = 0 > for(i in 1:length(data$S2)) { > if(!is.na(data$S2[i])) { > counter = counter + 1 > } > } > > I have imagined something like length(x,na.rm=TRUE).sum(is.na(data$s2)), using that the fact that TRUE/FALSE are coerced to 1/0.> How can I get values usually taken from tables like z-score, > values of t distribution etc.? I could not find them among > values mentioned in info file.Use qnorm/pnorm etc. Here's the examples from chapter 1 of MASS4 (a good reference!) The \s. environment includes the equivalent of a comprehensive set of statistical tables; one can work out $P$ values or critical values for a wide range of distributions (see Table~\ref{tab:dist.1} on page~\pageref{tab:dist.1}). \begin{session} 1 - pf(4.3781, 4, 76)&$P$ value from the ANOVA table above.\cr qf(0.95, 4, 76)&corresponding 5\% critical point.\cr \end{session} In R you could also use pf(4.3781, 4, 76, lower.tail=FALSE) although I tend to find that too much typing. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Matej, At 02:48 AM 10/23/2002 -0400, Matej Cepl wrote:>How can I get values usually taken from tables like z-score, >values of t distribution etc.? I could not find them among >values mentioned in info file.R has random-number generators and cumulative-probability, density, and quantile functions for all of the standard statistical distributions. The names are very regular -- for example, rt, pt, dt, and qt for the t-distribution, and rnorm, pnorm, dnorm, and qnorm for the normal. Try, for example, help(rt) for the details. Even better, look at section 8 (on probability distributions) of An Introduction to R, one of the manuals available on CRAN. I hope that this helps, John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Surely not the best method: length(which(is.na(data$S2)==TRUE)) On Wednesday 23 October 2002 08:48, Matej Cepl wrote:> Hi, > > how to do quickly equivalent of the following? > > counter = 0 > for(i in 1:length(data$S2)) { > if(!is.na(data$S2[i])) { > counter = counter + 1 > } > } > > I have imagined something like length(x,na.rm=TRUE). > > How can I get values usually taken from tables like z-score, > values of t distribution etc.? I could not find them among > values mentioned in info file. > > Thanks, > > Matej-- David Wartel -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
sum(is.na(data$S2))
[is.na returns TRUE/FALSE, which can also be added up as 0/1 values]
For your other question: see ?pt, ?pnorm (which will also give you
information about qt, qnorm, the quantile functions)
Or help.search("normal"), help.search("Student")
On Wed, 23 Oct 2002, Matej Cepl wrote:
> Hi,
>
> how to do quickly equivalent of the following?
>
> counter = 0
> for(i in 1:length(data$S2)) {
> if(!is.na(data$S2[i])) {
> counter = counter + 1
> }
> }
>
> I have imagined something like length(x,na.rm=TRUE).
>
> How can I get values usually taken from tables like z-score,
> values of t distribution etc.? I could not find them among
> values mentioned in info file.
>
> Thanks,
>
> Matej
>
>
--
318 Carr Hall bolker at zoo.ufl.edu
Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker
Box 118525 (ph) 352-392-5697
Gainesville, FL 32611-8525 (fax) 352-392-3704
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi,
length(data$S2[is.na(data$S2)]) #counts NA
length(data$S2[!is.na(data$S2)]) #counts NOT NA
is the easiest solution I know.
JS
- - - Original message: - - -
From: owner-r-help at stat.math.ethz.ch
Send: 23.10.2002 9:50:19
To: r-help at stat.math.ethz.ch
Subject: [R] Counting NA?
Hi,
how to do quickly equivalent of the following?
counter = 0
for(i in 1:length(data$S2)) {
if(!is.na(data$S2[i])) {
counter = counter + 1
}
}
I have imagined something like length(x,na.rm=TRUE).
How can I get values usually taken from tables like z-score,
values of t distribution etc.? I could not find them among
values mentioned in info file.
Thanks,
Matej
--
Matej Cepl, matej at ceplovi.cz, PGP ID# D96484AC
138 Highland Ave. #10, Somerville, Ma 02143, (617) 623-1488
The difference between death and taxes is death doesn't get worse
every time Congress meets
-- Will Rogers
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> -----Original Message----- > From: Matej Cepl [mailto:matej at ceplovi.cz] > Sent: Wednesday, October 23, 2002 10:53 AM > To: r-help at stat.math.ethz.ch > Subject: Re: [R] Counting NA? > > > On Wed, Oct 23, 2002 at 01:54:50PM +0200, David Wartel wrote: > > Surely not the best method: > > length(which(is.na(data$S2)==TRUE)) >For pure convenience, the gregmisc package includes a generic function 'nobs' which (for vectors) does sum(!is.na(x)). > library(gregmisc) > nobs( c(1,2,3,4,5,NA,NA,NA )) [1] 5> One this I was really surprised what that in order to get data > from the same data I have to use following construct constantly: > > sumyes <- length(data$S2[data$S2 == 1 & !is.na(data$S2)]) > > Isn't there any way how to say R, that I want eliminate NA data > for all my following calculations?Well, the simplest thing would be to create a new variable with the missing values removed, y <- na.omit(data$S2) which is pretty much equivalent to y <- data$S2[!is.na(data$S2)] in this case. For data frames it na.omit removes rows with any missing value, which is what we often desire:> data <- data.frame( x=rnorm(10), y=rnorm(10)) > data$y[3] <- NA > datax y 1 -1.40045667 0.1370657 2 0.16997476 0.4742317 3 0.66760007 NA 4 1.55857918 0.7787977 5 -0.70213268 -0.3806202 6 -1.03643219 0.4481208 7 -0.72489209 0.5521957 8 -0.86034676 1.3522844 9 0.50605859 1.5335474 10 -0.05295296 1.4871456> na.omit(data)x y 1 -1.40045667 0.1370657 2 0.16997476 0.4742317 4 1.55857918 0.7787977 5 -0.70213268 -0.3806202 6 -1.03643219 0.4481208 7 -0.72489209 0.5521957 8 -0.86034676 1.3522844 9 0.50605859 1.5335474 10 -0.05295296 1.4871456>-Greg LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._