mardi 30 mai 2017

For loops? Including rows in a dataframe by the missing values of factor levels

Good morning

I have a dataset of fisheries data with several variables that look like this:

ID              Day      Month   Year  Depth  Haul number  Count LengthClass     
H111200840       11        1     2008   -80       40        4      10-20
H111200840       11        1     2008   -80       40        15     20-30
H29320105        29        3     2010   -40       5         3      50-60
H29320105        29        3     2010   -40       5         8      60-70

The column ID is a unique ID made by paste the columns day,month,Year and Haul.number. As you can see for the same ID I have data of different Length Class. En each Haul, fish from different lengths are captured.

However, LengthClass is a factor variable with the following levels: 10-20, 20-30, 30-40, 40-50 and fish of a certain length class that is not captured in a Haul is not recorded in the dataset.

I need to include in the above data.frame example new rows for each ID with the missing levels of LengthClass.

The missing Length classes should have a Count of 0 but the rest of the variables have to be the same.

This is an example of what I would like

 ID              Day      Month   Year  Depth  Haul number  Count LengthClass     
  H111200840       11        1     2008   -80       40        4      10-20
  H111200840       11        1     2008   -80       40        15     20-30
  H111200840       11        1     2008   -80       40        0      30-40
  H111200840       11        1     2008   -80       40        0      40-50
  H111200840       11        1     2008   -80       40        0      50-60
  H29320105        29        3     2010   -40       5         3      40-60
  H29320105        29        3     2010   -40       5         8      50-60
  H29320105        29        3     2010   -40       5         0      10-20
  H29320105        29        3     2010   -40       5         0      20-30
  H29320105        29        3     2010   -40       5         0      30-40

Is there anyway to do this? I have tried for loops with if arguments but with no luck and also the example of this post:

Thanks for any advice in advance

Aucun commentaire:

Enregistrer un commentaire