-3

I have a dataf rame with 5 columns and 14 rows. I would like to sum all the values of the rows "Barnacle nauplii" and "Echinoderm larvae" to create a new row called "Meroplanktonic larvae". Does anyone have any suggestions on how I can achive this?

My data frame looks like this:

enter image description here

user438383
  • 5,716
  • 8
  • 28
  • 43
HMatt
  • 1
  • 2
    Please provide a reproducible code for your dataset. You can use `dput(your_dataset)` and share it in the question. – Maël Apr 22 '22 at 10:05

1 Answers1

0

Since you don't provide a reproducible code for your dataset, it is a bit hard to answer, but something like this should work:

aggregate(df, list(Class = replace(df$Class, df$Class %in% c("Barnacle nauplii","Echinoderm larvae"), "Meroplanktonic larvae")), sum)
Maël
  • 45,206
  • 3
  • 29
  • 67
  • Thank you. However, I get this error: Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument – HMatt Apr 22 '22 at 10:18
  • 2
    @HMatt unless you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) then it is very hard to debug any errors you get. – user438383 Apr 22 '22 at 10:22
  • @HMatt can you provide a reproducible example of your data ? – Maël Apr 22 '22 at 13:40