如何基于r中数据帧中的另一个变量向数据帧添加二进制变量?

如何基于r中数据帧中的另一个变量向数据帧添加二进制变量?,r,performance,dataframe,R,Performance,Dataframe,我的数据(train)是一个443402 x 27数据帧,我已经将一个新的二进制变量train$researchedplan初始化为“1”。共有64673个唯一的train$customer_ID(每个客户在数据框中输入随机次数,但顺序不同,即第一个客户有前9行,第二个客户有后6行,以此类推) 我还有一个向量(diff_than_researched),它由一系列独特的train$customer_ID组成,确定哪些客户没有研究特定的计划。 对于diff_than_中与train$custome

我的数据(train)是一个443402 x 27数据帧,我已经将一个新的二进制变量train$researchedplan初始化为“1”。共有64673个唯一的train$customer_ID(每个客户在数据框中输入随机次数,但顺序不同,即第一个客户有前9行,第二个客户有后6行,以此类推)

我还有一个向量(diff_than_researched),它由一系列独特的train$customer_ID组成,确定哪些客户没有研究特定的计划。 对于diff_than_中与train$customer_ID中的字符串匹配的字符串,我希望train$researchedplan中客户的所有条目都为“0”。 e、 g:

因此,对于所有“1000019”条目,我希望train$researchedplan等于“0”

现在,我可以通过一个“for循环”来完成这一切,但是循环这么多条目需要太长时间:

因为(我在1:17210中){
train$researchedplan[train$customer\u ID==diff\u than\u researched[i]]使用略微不同的数据以提高可读性,并在所研究的计划中获得一些0

train

##    customer_ID
## 1     10000000
## 10    10000005
## 24    10000013
## 28    10000014
## 5     10000019    

train$researchedplan <- as.numeric(!train$customer_ID %in% diff_than_researched)

##    customer_ID researchedplan
## 1     10000000              1
## 10    10000005              1
## 24    10000013              1
## 28    10000014              1
## 5     10000019              0
列车
##客户识别码
## 1     10000000
## 10    10000005
## 24    10000013
## 28    10000014
## 5     10000019    

train$researchedplan
train$researchedplan@JakeBurkhead为什么
在%
中的
%评估后应用,而不是作为
!train$customer
?@rawr.
特殊操作员(包括%%和%/%)
走在
前面!否定
从未注意过。很好info@jake看起来像答案-请复制/粘贴到答案。
> head(diff_than_researched)
>[1] "10000019" "10000033" "10000036" "10000037" "10000055" "10000075"
train

##    customer_ID
## 1     10000000
## 10    10000005
## 24    10000013
## 28    10000014
## 5     10000019    

train$researchedplan <- as.numeric(!train$customer_ID %in% diff_than_researched)

##    customer_ID researchedplan
## 1     10000000              1
## 10    10000005              1
## 24    10000013              1
## 28    10000014              1
## 5     10000019              0