[英]Ifelse statment across multiple rows
Looking to add a column based on the values of two columns, but over more than one row. 希望根据两列的值添加列,但是多行。
Example Dataset Code: 示例数据集代码:
A = c(1,1,1,2,2,2,3,3,3,4,4)
B = c(1,2,3,1,2,3,1,2,3,1,2)
C = c(0,0,0,1,0,0,1,1,1,0,1)
data <- data.frame(A,B,C)
Dataset: 数据集:
A B C
1 1 1 0
2 1 2 0
3 1 3 0
4 2 1 1
5 2 2 0
6 2 3 0
7 3 1 1
8 3 2 1
9 3 3 1
10 4 1 0
11 4 2 1
Ifelse statements: Ifelse声明:
What I am trying to achieve is "Create column D
.If column C
== 1 in any row where column A
== x, column D
= 1. Else column D
== 0" 我想要实现的是“创建列
D
如果列C
== 1,在列A
== x,列D
= 1的任何行中。列D
== 0”
Desired Output: 期望的输出:
A B C D
1 1 1 0 0
2 1 2 0 0
3 1 3 0 0
4 2 1 1 1
5 2 2 0 1
6 2 3 0 1
7 3 1 1 1
8 3 2 1 1
9 3 3 1 1
10 4 1 0 1
11 4 2 1 1
What I've done: 我做了什么:
I've thought about it today but can't come up with a logical answer, I've tried looking at the data in long and wide formats but nothings jumped out. 我今天已经考虑过了,但无法得出合乎逻辑的答案,我已经尝试过查看长格式和宽格式的数据,但没有想到就跳出来了。
Note: In actual application the number of times x appears in column C
is not equal (some contain one repeat in the dataset, others contain 20). 注意:在实际应用中,x在
C
列中出现的次数不相等(某些在数据集中包含一个重复,其他包含20个)。
# just check using any() if any group has a single row with C==1
library(dplyr)
data %>% group_by(A) %>% mutate(D = as.numeric(any(C==1)))
library(data.table)
data[, D:=as.numeric(any(C==1)), by = .(A)]
# A B C D
#1 1 1 0 0
#2 1 2 0 0
#3 1 3 0 0
#4 2 1 1 1
#5 2 2 0 1
#6 2 3 0 1
#7 3 1 1 1
#8 3 2 1 1
#9 3 3 1 1
#10 4 1 0 1
#11 4 2 1 1
Easy with data.table
容易使用
data.table
library(data.table)
data <- data.table(data)
x=2
data[,D:=ifelse(!A==x,ifelse(C==1,1,0),0)]
data
We can use ave
from base R
我们可以使用
base R
ave
data$D <- with(data, as.integer(ave(C==1, A, FUN=any)))
data
# A B C D
#1 1 1 0 0
#2 1 2 0 0
#3 1 3 0 0
#4 2 1 1 1
#5 2 2 0 1
#6 2 3 0 1
#7 3 1 1 1
#8 3 2 1 1
#9 3 3 1 1
#10 4 1 0 1
#11 4 2 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.