From Stats Beerz on 2015-03-23. Here’s the question: How can you
substitute values in one column based on values in another column? There
are many many ways. Here are a few,
Create some fake data:
Different ways to select only rel == "no":
Different ways of selecting the troph column with rel == "no":
Use the above to come up with different ways of substituting NA for
all values of troph where rel == "no":
Pipes, pipes, pipes: (The %>% is the pipe symbol from the magrittr
package, which is loaded via the dplyr package, which also loads
mutate(). mutate() is like transform() but allows you to add
columns that build on columns you just built.)
This is just getting silly, but the magrittr package has other kinds of
pipes. One kind is %<>% which assigns the value to the object of the
same name to the left of the pipe. E.g.: