I have a dataframe (classification of substances with the classes) in the following format:
1 | 2 | 3 | 4 |
---|---|---|---|
Organic compounds | Benzenoids | Benzene | NA |
Organic compounds | Benzenoids | Benzene | NA |
Organic compounds | Organic oxygen compounds | NA | NA |
NA | NA | NA | NA |
Organic compounds | Benzenoids | NA | NA |
I need to add a new column with the count of the rows with the same value in all 4 columns (rows with same value in column 1-4):
1 | 2 | 3 | 4 | count |
---|---|---|---|---|
Organic compounds | Benzenoids | Benzene | NA | 2 |
Organic compounds | Organic oxygen compounds | NA | NA | 1 |
NA | NA | NA | NA | 1 |
Organic compounds | Benzenoids | NA | NA | 1 |
My problem is, to add a new column in the dataframe, which count the amount of multiple rows (1-4) at the same time.
For 1 row I have used table()
:
data.frame(table(df$"1")
But if I use more than one row, there is not the right output data.frame(table(df$"1",df$"2",df$"3", df$"4"))
.
I also tried it with count()
I found no solution for this specific problem here. So I need your help, please. Thanks!