mercredi 1 août 2018

Looping through a vector, creating a new variable which is the first vector minus 2 other vectors when none of them are NA

Assuming the following dataset:

Company  Sales  COGS  Staff
A        100      50     25
B        200      NA    100
C         NA      50     25
D         75      50     25
E        125     100     NA

I would like to create a new variable called profit which is Sales- COGS -Staff, if neither of those variables is NA. The desired output would be as follows:

Company  Sales  COGS  Staff  Profit
A        100      50     25      25
B        200      NA    100      NA
C         NA      50     25      NA
D         75      50     25       0
E        125     100     NA      NA

I started with something like:

# Creating the profit column (should be unnecessary right?)
df$Profit <- NA
# For each row in the sales column/vector
for(i in df$Sales){
# For each row in the sales column/vector
if(!is.na(df$Sales) & !is.na(df$COGS) & !is.na(df$Staff)){
df$Profit <- df$Sales - (df$COGS + df$Staff)
} else {
df$Profit <- NA
}}

Which does not give an error, but it makes R go a bit haywire. Is there a more efficient way to do this?

Aucun commentaire:

Enregistrer un commentaire