I think the most robust method for you will involve having your species-value list on another layer as a CSV or similar - in other words, a lookup table. Trying to match values in expressions will be incredibly inefficient, difficult to edit, and prone to error.
It should be easy to create such a table in any spreadsheet program, even a basic text editor, then import it into QGIS using Add Delimited Text Layer. (Make sure to trim fields so that no extra spaces make their way in)
Once you have that you could use the following expression (built on @csk's excellent suggestion to use arrays) to return the values required and sum them together. If one or more species cannot be found in the lookup table it will return a
NULL which should prompt the user to check the data.
This is the lookup table I have set up called
taxa_csv in the layers list (note the name).
This is the expression I used in field calculator (note lookup table name is in expression).
Result - note
NULLs for entries where species not in lookup table.
- The lookup table name (
taxa_csv) and corresponding column names (
value) in the expression are case-sensitive. If you rename the lookup table and/or column names, just remember to change the names accordingly in this expression.
- Even if your source values are all text (e.g. CSV) the expression will convert it to a numeric format (
1/3 won't work - I suggest converting this to a decimal number)
- If you want to treat species not found as
0 rather than
NULL replace accordingly in the expression. I don't recommend this though as it can be misleading
- Does anyone know a good way to sum everything in an array in QGIS Field Calc? Had to kludge it together by converting back to a string and replacing commas with plus signs then evaluating it with