Hello, I wish to count how often zero (0) appears in the vector test. test [1] 1 1 1 1 1 1 2 1 1 1 0 2 0 1 1 0 0 0 1 1 1 0 1 2 1 1 1 1 1 1 I think of something like ...> sapply (test, function (x) if (x==0 ...... but actually I dont know how to carry on. Could anybody give me a hint? Thanks Hermann [[alternative HTML version deleted]]
sum(test == 0) On Thu, Jan 3, 2013 at 5:49 PM, Hermann Norpois <hnorpois at googlemail.com> wrote:> Hello, > > I wish to count how often zero (0) appears in the vector test. > > test > [1] 1 1 1 1 1 1 2 1 1 1 0 2 0 1 1 0 0 0 1 1 1 0 1 2 1 1 1 1 1 1 > > > I think of something like ... > >> sapply (test, function (x) if (x==0 ... > > ... but actually I dont know how to carry on. > > Could anybody give me a hint? > > Thanks Hermann >--- Sarah Goslee http://www.functionaldiversity.org
Hi, YOu could use ?count() from library(plyr) or ?table() or ?sum() count(test)[,2][count(test)[,1]==0] #[1] 6 table(test)["0"] #0 #6 A.K. ----- Original Message ----- From: Hermann Norpois <hnorpois at googlemail.com> To: r-help at r-project.org Cc: Sent: Thursday, January 3, 2013 5:49 PM Subject: [R] count appearence of zero in a vector Hello, I wish to count how often zero (0) appears in the vector test. test [1] 1 1 1 1 1 1 2 1 1 1 0 2 0 1 1 0 0 0 1 1 1 0 1 2 1 1 1 1 1 1 I think of something like ...> sapply (test, function (x) if (x==0 ...... but actually I dont know how to carry on. Could anybody give me a hint? Thanks Hermann ??? [[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.
Hi Hermann, You may want to use ?which, to store the index as well (might be handy in debugging or some other purposes if zeros has some special meaning) : test <- c(1, 1, 1 , 1 , 1, 1, 2, 1, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 2, 1, 1, 1, 1, 1, 1) length(which(test==0)) But be careful when using == 0. If you are sure that elements are all integers I prefer this length(which(test <1)). Or a safe tolerance value from .Machine, if you are dealing with double numbers. length(which(test < .Machine$double.xmin)) -m
As long as there are no negative numbers, method2 and 3 works: ?test[1]<- -1 length(which(test==0)) #[1] 6 ?length(which(test<1)) #[1] 7 ?length(which(test < .Machine$double.xmin)) #[1] 7 length(which(abs(test)<1)) #[1] 6 ?length(which(abs(test) < .Machine$double.xmin)) #[1] 6 A.K. ----- Original Message ----- From: "Suzen, Mehmet" <msuzen at gmail.com> To: Hermann Norpois <hnorpois at googlemail.com> Cc: R help <r-help at r-project.org> Sent: Friday, January 4, 2013 12:27 AM Subject: Re: [R] count appearence of zero in a vector Hi Hermann, You may want to use ?which, to store the index as well (might be handy in debugging or some other purposes if zeros has some special meaning) : test <- c(1, 1, 1 , 1 , 1, 1, 2, 1, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 2, 1, 1, 1, 1, 1, 1) length(which(test==0)) But be careful when using == 0. If you are sure that elements are all integers I prefer this length(which(test <1)). Or a safe tolerance value from .Machine, if you are dealing with double numbers. length(which(test < .Machine$double.xmin)) -m ______________________________________________ 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.
I am always reserved about types and not sure how R auto casting works internally. In a large code using many different packages, I think being reserved about this would not hurt. Also, are there anyway to force R to be "strongly typed" similar to Occaml etc... mem On 4 January 2013 16:35, arun <smartpink111 at yahoo.com> wrote:> > As long as there are no negative numbers, method2 and 3 works: > test[1]<- -1 > length(which(test==0)) > #[1] 6 > length(which(test<1)) > #[1] 7 > length(which(test < .Machine$double.xmin)) > #[1] 7 > > length(which(abs(test)<1)) > #[1] 6 > length(which(abs(test) < .Machine$double.xmin)) > #[1] 6 > A.K. > > > > > ----- Original Message ----- > From: "Suzen, Mehmet" <msuzen at gmail.com> > To: Hermann Norpois <hnorpois at googlemail.com> > Cc: R help <r-help at r-project.org> > Sent: Friday, January 4, 2013 12:27 AM > Subject: Re: [R] count appearence of zero in a vector > > Hi Hermann, > > You may want to use ?which, to store the index as well (might be handy > in debugging or some other > purposes if zeros has some special meaning) : > > test <- c(1, 1, 1 , 1 , 1, 1, 2, 1, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, > 1, 1, 0, 1, 2, 1, 1, 1, 1, 1, 1) > length(which(test==0)) > > But be careful when using == 0. If you are sure that elements are all > integers I prefer this > > length(which(test <1)). > > Or a safe tolerance value from .Machine, if you are dealing with double numbers. > > length(which(test < .Machine$double.xmin)) > > > -m > > ______________________________________________ > 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. >