[英]Add a column based on a condition from other columns
I have this table and I would like to add the colum "New column" based on a condition. 我有此表,我想根据条件添加“新列”列。 The condition is that when in the column 3, we have "Peter", then in the "New col" I want to have the result of the col2.
条件是在第3列中有“ Peter”,然后在“ New col”中有col2的结果。 This value would be the same for the same ID.
对于相同的ID,该值将相同。 In the "New Col", you can see the desire result.
在“新列”中,您可以看到愿望结果。
ID Col2 Col3 New Col
1 B Peter B
1 A Matt B
2 B Peter B
2 B Matt B
2 A Matt B
3 C Peter C
This is what I have. 这就是我所拥有的。
for (j in 2:(j-1)){
if (df$Col3[j] == "Peter"){
df$Newcol[j] = df$Col2[j]
} else {
df$Newcol[j] = df$Newcol[j-1]
}
}
But instead of getting the rigth values, I get numbers "6" and "9". 但是我没有得到严格的值,而是得到了数字“ 6”和“ 9”。
Any suggestions? 有什么建议么? Thanks a lot
非常感谢
Using data.table
使用
data.table
library(data.table)
setDT(data)
data[,new:= if(any(Col3 == "Peter")) Col2[which(Col3 == "Peter")] else NA, by = ID]
# ID Col2 Col3 new
#1: 1 B Peter B
#2: 1 A Matt B
#3: 2 B Peter B
#4: 2 B Matt B
#5: 2 A Matt B
#6: 3 C Peter C
Using base R lapply
使用基础R
lapply
do.call(rbind,
lapply(split(data, data$ID),
function(x){ if(any(x$Col3 == "Peter")){
x$new = x$Col2[which(x$Col3 == "Peter")];
x}}))
# ID Col2 Col3 new
#1.1 1 B Peter B
#1.2 1 A Matt B
#2.3 2 B Peter B
#2.4 2 B Matt B
#2.5 2 A Matt B
#3 3 C Peter C
data 数据
data = structure(list(ID = c(1L, 1L, 2L, 2L, 2L, 3L), Col2 = structure(c(2L,
1L, 2L, 2L, 1L, 3L), .Label = c("A", "B", "C"), class = "factor"),
Col3 = structure(c(2L, 1L, 2L, 1L, 1L, 2L), .Label = c("Matt",
"Peter"), class = "factor")), .Names = c("ID", "Col2", "Col3"
), class = "data.frame", row.names = c(NA, -6L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.