samedi 17 mars 2018

Error when using "ifelse" to create new column of data set based on formula using variables within datased

I have a data set that looks like this:

    'data.frame':   25952 obs. of  12 variables:
    $ Year        : int  2016 2016 2016 2016 2016 2016 2016 2016 2016        2016 ...
    $ Date        : Factor w/ 15 levels "","2016-05-26",..: 2 2 2 2 2 2 2 2 2 2 ...
    $ Location    : Factor w/ 22 levels "Coquet Island",..: 3 3 3 3 3 3 3 3 3 3 ...
    $ Group       : Factor w/ 3 levels "vis","worm","inktvis": 1 1 1 1 1 1 1 1 1 1 ...
    $ Part        : Factor w/ 8 levels "kaak","otoliet",..: 3 3 3 3 3 3 3 3 3 3 ...
    $ Part_type   : Factor w/ 6 levels "","asteriscus",..: 4 4 4 4 4 4 4 4 4 4 ...
    $ Taxon       : Factor w/ 20 levels "","Ansjovis",..: 19 19 19 19 19 19 19 19 19 19 ...
    $ Further_ID  : Factor w/ 12 levels "","kabeljauwachtige?",..: 5 8 5 5 8 8 8 5 8 8 ...
    $ Length      : num  3021 2897 3203 3118 2509 ...
    $ Width       : num  1483 1511 1427 1387 1276 ...
    $ YearLocation: chr  "2016   Markenje" "2016   Markenje" "2016   Markenje" "2016   Markenje" ...
    $ Family      : Factor w/ 5 levels "Nereidae","Invertebrates",..: 5 5 5 5 5 5 5 5 5 5 ...

I am trying to calculate total fish length for every sample using a formula based on "taxon" and "length" values. This is an example of one of the codes I have been using (the formula varies slightly based on Taxon):

    dieet.GS.EU$FL <- ifelse(dieet.GS.EU$Taxon %in% c("zandspieringachtige"), ((0.000006 * (dieet.GS.EU$Length ^ 2) + (0.0311 * dieet.GS.EU$Length) + 24.161)), dieet.GS.EU$FL)

When I try to run this I receive the error message:

    Error in ans[!test & ok] <- rep(no, length.out = length(ans))[!test &  : 
    replacement has length zero
    In addition: Warning message:
    In rep(no, length.out = length(ans)) :
    'x' is NULL so the result will be NULL

Can anyone please help me figure out the issue here? I believe it has to do with my data set rather than the code I am running, but I am not very experienced in R so I am not sure. Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire