2016-07-25 108 views
4

我有一個數據幀,像這樣:R:有條件地替換在列的子集的值

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 

我想有條件地取代的列[4:6]的NA,基於變量「運動」和「合同開始」。 因此,例如:

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

意願指數中,我的條件得到滿足的所有行。 是否有一段簡單的代碼添加到上面,在給定上述條件的情況下,將用NA替換df [4:6]? 我想結束這樣的事情:

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 

謝謝! A.

回答

6

你可以簡單地指定的行和列,你想用NA來替代,並分配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 
+0

謝謝,正是我想要的。 –

3
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)]