如果其他列相等,则生成列

问题描述 投票:0回答:1

我有两个数据帧:

df1 <- data.frame('ID'=c(1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10), 
              'invoice'=c(24000, 21000, 25000, 21000, 26000, 27000,
               28000, 29000, 30000, 31000, 21000, 32000, 33000),
              'settle'=c(40000, 40000, 41000, 41000, 42000, 43000, 44000,
               45000, 46000, 47000, 47000, 48000, 49000), 
              'amount'=c(10, 10, 20, 10, 30, 10, 20, 30, 10, 20, 
               10, 30, 10), 
              'reason'=c(4, 5, 5, 5, 9, 4, 5, 9, 4, 5, 5, 15, 8))

和:

 df2 <- data.frame('ID'=c(1, 2, 2, 4, 5, 7, 8, 11, 12), 
              'invoice'=c(40000, 41000, 39000, 43000, 44000, 46000, 
               47000, 40000, 41000),
              'settle'=c(24000, 25000, 21000, 27000, 28000, 30000, 
               31000, 24000, 25000),
              'amount'=c(10, 20, 20, 10, 20, 10, 20, 10, 10), 
              'reason'=c(4, 5, 5, 4, 5, 4, 5, 4, 4))

DF1:

ID invoice settle amount reason
 1   24000  40000     10      4
 1   21000  40000     10      5
 2   25000  41000     20      5
 2   21000  41000     10      5
 3   26000  42000     30      9
 4   27000  43000     10      4
 5   28000  44000     20      5
 6   29000  45000     30      9
 7   30000  46000     10      4
 8   31000  47000     20      5
 8   21000  47000     10      5
 9   32000  48000     30     15
10   33000  49000     10      8

DF2:

ID invoice settle amount reason
 1   40000  24000     10      4
 2   41000  25000     20      5
 2   39000  21000     20      5
 4   43000  27000     10      4
 5   44000  28000     20      5
 7   46000  30000     10      4 
 8   47000  31000     20      5
11   40000  24000     10      4
12   41000  25000     10      4

所以我想从以下条件在df1中生成一个虚拟变量:

if df1$ID == df2$ID
if df1$settle == df2$invoice
if df1$amount == df2$amount
if df1$reason == df2$reason

因此,如果满足条件,我的新列应该等于1,否则为0。

带有新变量的df1看起来像这样:

 ID invoice settle  amount reason  newvar
 1   24000  40000     10      4      1
 1   21000  40000     10      5      0
 2   25000  41000     20      5      1
 2   21000  41000     10      5      0
 3   26000  42000     30      9      0
 4   27000  43000     10      4      1
 5   28000  44000     20      5      1
 6   29000  45000     30      9      0
 7   30000  46000     10      4      1
 8   31000  47000     20      5      1
 8   21000  47000     10      5      0
 9   32000  48000     30     15      0
10   33000  49000     10      8      0

我试过了:

 df1$newvar <- ifelse(df1$ID == df2$ID & 
                      df1$settle == df2$invoice &
                      df1$amount == df2$amount &
                      df1$reason == df2$reason, 1, 0)

我收到警告信息:

 "longer object length is not a multiple of shorter object length"

所以我猜不出因为我的两个数据帧大小不同(df1中的ID比df2中的ID多),因此无法实现ifelse。

你能帮我解决这个问题吗?

在SPSS或Stata中,我只使用IF命令,但R对我来说是新手!

编辑

我已经改变了我的测试数据帧,因此它们与我使用的数据帧更相似。我还添加了一个带有新变量的df1视图。

r if-statement dataframe
1个回答
1
投票

错误是因为您的两个数据帧具有不同的行数。

尝试首先按条件连接两个表,然后在df1上创建新列。

df1<-left_join(df1,df2,by=c("ID"="ID","settle"="invoice","amount"="amount","reason"="reason"))
df1<-df1 %>% mutate(newvar=ifelse(is.na(settle.y),0,1)) %>%
             select(-settle.y)
© www.soinside.com 2019 - 2024. All rights reserved.