0

I have this array:

[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1
 [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [75] 1 1 2 1 2 2 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2
[112] 2 1 1 2 2 2 2 2 2 1 2 1 1 2 1 1 2 1 1 2 1 1 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2

And I want to count the number of occurrences of '1' and '2'. From [1] to [70] and from [71] to the end.

I tried :

sum(x==1)

But this for all.How can I select lines?

yokie
  • 153
  • 9

1 Answers1

1

the function sum {base} should return the sum of all the values present in its arguments

you could define the arguments the following way: with x[a:b] you can set boundaries (for example a=1 and b=10, will set the area from [1] to[10]); with the operator == you can check if one specific value c is present between your boundaries ... e.g.: x[a:b]==c if you want to look for more than one value ( for example c & d , where c==1 and d==2 , you can (for example) use a simple addition to sum up your results:

Now you can just say: sum(x[a:b]==c) + sum(x[a:b]==c) Where a&b are your boundaries and c&d are the values you want to compare.

LPH
  • 1,275
  • 9
  • 16
  • Hi, I have another question. I want to get this values but automatically. I mean, is there any function in R which will automatically give me number of occurences of the maximum value? If it is '2' automatically return the numbers of '2' in the selected vector.Thanks – yokie May 19 '15 at 13:48