tally number occurances in one data.frame against a number vector R -


I have a line per overview and a field in which this observation was made, in which there is a line. Samples are from 1 to 20. Some samples are non-existent and others have many observations. My aim is to count the sample overview - which I used to use the plier and counting work, however, the samples that do not have any observation are not included in the output (as they are not found in observation data frames). My thoughts were for matching numbers in the obs. Data.Write against number vector r (CEC (1:20).

What I found:

  Library (plyr) id = c (1,1, 1,4,4,5,6,6,8,8,10,15,15,17,18,21,21)  

Sample IDs with these comments are sample 1 : 20 by

  obs = sample (seq (5, 50, by = 3), size = 17, substituted = TRUE) df = data.frame (id, obs) outside & lt; -Count (DF $ ID)  

Only samples with comments are included. Samples 2,3,7,9,11,12,13,14,16,17,19,20 Everyone had 0 abs.I want to include them in the output.

An option is to make sample ID a factor, and then use table () to get your calculation :

  id = c (1,1,1,4, 4,5,6,6,8,10,15,15,17,18,21,21) obs = Sample (Seq (5, 50, = 3), Size = 17, Replace = TRUE) df = Data.frame (id, obs) df $ id & lt; -factor (df $ id, level = c (1: 21) out & lt; -table (df $ id) out 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 3 0 0 2 1 2 0 2 0 1 0 0 0 0 2 0 1 1 0 2 2  

level in the argument, the factor indicates all the levels, even those data Not found in May include I'm assuming that you want the ID includes 21


Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -