I need to set certain numeric values in one column of my data frame to zero, if in another column they have a certain factor level.
My dataframe df looks something like:
Items Store.Type
5 A
4 B
3 C
6 D
3 B
7 E
What I want to do is make Items = 0, for all rows where Store.Type = "A" or "C"
I'm very new to R, but figured this this would be a conditional statement of the form "If Store.Type A then Items <- 0" (and then repeat for Store.Type C), but I didn't understand the ?"if"
page at all. I tried:
df$ItemsFIXED <- with(df, if(Store.Type == "A")Items <-0)
and got the warning message:
Warning message:
In if (Store.Type2 == "Chain - Brand") Total.generic.items <- 0 :
the condition has length > 1 and only the first element will be used`
So I noticed here, the following:
if
is a control flow statement, taking a single logical value as an argumentifelse
is a vectorised function, taking vectors as all its arguments.
So figuring I need ifelse
to do the whole column and being able to understand the ?ifelse
page, I tried to do "If Store.Type A then Items <- 0 else do nothing". In fact I wanted it nested, so I tried the following code (creating a new column for now so I don't mess up my data, but eventually it will overwrite the Items data)
df$ItemsFIXED <- with(df, ifelse(Store.Type == "A", Items <-0,
ifelse(Store.Type == "C", Items <-0,)))
and got the following error:
Error in ifelse(Store.Type2 == "Franchise - Brand", Total.generic.items <- 0, :
argument "no" is missing, with no default
But if I put anything in for no
it simply writes over the values which are correct. I tried putting Items
and Items <- Items
in to say "else leave Items as Items" as in the following, but this just changed everything to zero.
df$ItemsFIXED <- with(df, ifelse(Store.Type == "A", Items <-0,
ifelse(Store.Type == "C", Items <-0,Items)))
Is there a way to tell ifelse
to do nothing, or is there an easier way to do this?