mardi 6 mars 2018

Calculations with conditions in R

I have the following dataframe:

   A   B   C   
1  SS  2   20   
2  TT  3   15   
3  SS  10  150
4  TT  12  121
5  UU  1   2
6  SS  4   48
7  TT  5   40
8  UU  3   12
9  UU  10  60

When column A is SS and if B is 2.99 and below, between 3 and 5.99, 6 and above, multiply values in column B by 10, 12 and 15 to give 20, 150 and 48 in column C for rows 1, 3 and 6 respectively.

When column A is TT and if B is 3.99 and below, between 4 and 6.99, 7 and above, multiply values in column B by 5, 8 and 11 to give 15, 121 and 40 in column C for rows 2, 4 and 7 respectively.

When column A is UU and if B is 1.99 and below, between 2 and 7.99, 8 and above, multiply values in column B by 2, 4 and 6 to give 2, 12 and 60 in column C for rows 5, 8 and 9 respectively.

I would much appreciate any guidance how I can do this in R.

Many thanks in advance for any suggestions provided.

Aucun commentaire:

Enregistrer un commentaire