R: conditionally replace values ​​in a subset of columns

I have a data beat:

sport   contract start contract end visits spends purchases
basket   2013-10-01     2014-10-01   12      14      23
basket   2014-02-12     2015-03-03   23      11      7
football 2015-02-12     2016-03-03   23      11      7
basket   2016-07-17     2013-09-09   12       7      13

I would like to conditionally replace the [4: 6] columns with NA, based on the variables “sport” and “start of contract”. For example:

i1 <- which(df$sport =="basket" & df$contract_start>="2014-01-01")

will index all rows in which my conditions are met. Is there a simple code snippet to add to the above that will replace df [4: 6] with NA, given the above conditions? I would like to get something like this:

sport   contract start contract end visits spends purchases
basket   2013-10-01     2014-10-01   12      14      23
basket   2014-02-12     2015-03-03   NA      NA      NA
football 2015-02-12     2016-03-03   23      11      7
basket   2016-07-17     2013-09-09   NA      NA      NA

Thank! AND.

+4
source share
2 answers

You can simply specify the rows and columns that you want to replace NA and assign to it NA:

df[df$sport =="basket" & df$contract_start>="2014-01-01", 4:6] <- NA

df
#      sport contract_start contract_end visits spends purchases
# 1   basket     2013-10-01   2014-10-01     12     14        23
# 2   basket     2014-02-12   2015-03-03     NA     NA        NA
# 3 football     2015-02-12   2016-03-03     23     11         7
# 4   basket     2016-07-17   2013-09-09     NA     NA        NA
+6
source
library("data.table")
setDT(df)
df[i = sport == "basket" & contract_start >= "2014-01-01", 
   j = c("visits", "spends", "purchases") := NA]

> df
      sport contract_start contract_end visits spends purchases
1:   basket     2013-10-01   2014-10-01     12     14        23
2:   basket     2014-02-12   2015-03-03     NA     NA        NA
3: football     2015-02-12   2016-03-03     23     11         7
4:   basket     2016-07-17   2013-09-09     NA     NA        NA

my_cols:

my_cols <- names(df)[4:6]
df[i = sport == "basket" & contract_start >= "2014-01-01", 
   j = (my_cols) := .(NA)]
+3

Source: https://habr.com/ru/post/1649068/


All Articles