简体   繁体   English

R(指标)中的条件语句基于与另一个数据集的匹配值

[英]Conditional Statement in R (indicator) based off matching values to another dataset

I have two datasets 我有两个数据集

dataset1 with column fruit, customer_num 具有列结果为customer_num的数据集1

dataset2 with column fruit2, customer_num 具有水果2列,customer_num的数据集2

So lets say I do a left join with dataset 1 to dataset 2, using customer_num as the joiner. 因此,可以说我使用customer_num作为联接器,对数据集1进行左联接到数据集2。 Now I got a dataset with fruit and fruit2 as column variables. 现在,我得到了一个数据集,其中有fruit和fruit2作为列变量。

How can a create an indicator to say if fruit==fruit2 then 1 else 0 ? 如何创建一个指标来说明是否fruit == fruit2然后1 else 0?

ifelse would be easiest, assuming it is in the same dataframe. 假设它位于同一数据帧中,则ifelse最简单。 Example using the dplyr package 使用dplyr包的示例

    dataset1 %>%
    mutate(Match=ifelse(fruit==fruit2,1,0))

This will create a column called Match and do 1 if they match, 0 if they do not 这将创建一个名为Match的列,如果匹配则执行1,如果不匹配则执行0

You could do it like this (my example): 您可以这样做(我的例子):

# 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.

相关问题 使用基础 R 根据另一个数据集中的值索引替换数据集中的值 - Replace values in a dataset based off an index of values in another using base R 基于 r 中的行值的条件 if 语句 - Conditional if statement based on row values in r 将数据集与另一个数据集匹配,并使用R指定相应的值 - Matching a dataset with another dataset and assigning the respective values using R 使用基数 R 根据另一个条件中的值从表中删除行 - Removing rows from a table based off values in another conditional on a value in a separate column using base R 基于另一个在 dataframe 中创建新列,并与 R 中的另一个数据集匹配 - Create new column in dataframe based on another and matching to another dataset in R 如何根据R中另一个数据集的分布对数据进行采样 - How to sample data based off the distribution of another dataset in R 根据另一列 R 的条件语句创建新列 - Create a new column based on conditional statement of another column R 如何基于R中的if条件语句重新编码一组变量中的负值? - How to recode negative values in a set of variables based on if conditional statement in R? 如何根据 R 中另一列的值填写空白 - How to fill in blanks based off another column's values in R 将数据集中的值与R中另一个数据集中的值相乘 - Multiply values in a dataset by values in another dataset in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM