[英]Replace values in a dataset based off an index of values in another using base R
[英]Conditional Statement in R (indicator) based off matching values to another dataset
我有兩個數據集
具有列結果為customer_num的數據集1
具有水果2列,customer_num的數據集2
因此,可以說我使用customer_num作為聯接器,對數據集1進行左聯接到數據集2。 現在,我得到了一個數據集,其中有fruit和fruit2作為列變量。
如何創建一個指標來說明是否fruit == fruit2然后1 else 0?
假設它位於同一數據幀中,則ifelse最簡單。 使用dplyr包的示例
dataset1 %>%
mutate(Match=ifelse(fruit==fruit2,1,0))
這將創建一個名為Match的列,如果匹配則執行1,如果不匹配則執行0
您可以這樣做(我的例子):
# I've created example of customer_num where I presumed that this are numbers
fruit <- data.frame(customer_num = c(1, 2, 3, 4, 5, 6))
fruit2 <- data.frame(customer_num = c(1, 2, 3, 10, 11, 12))
# Vector in data frame
df <- data.frame(fruit, fruit2)
# And match values / Indicator
dat<-within(df,match <- ifelse (fruit == fruit2,1,0))
# Output
customer_num customer_num.1 customer_num
1 1 1 1
2 2 2 1
3 3 3 1
4 4 10 0
5 5 11 0
6 6 12 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.