【发布时间】:2019-01-31 04:51:55
【问题描述】:
我正在尝试过滤这个名为 df 的数据框
structure(list(ï..ID = structure(c(2L, 1L, 4L, 6L, 3L, 7L, 5L,
8L), .Label = c("Jay ", "Jim", "Jim ", "John ", "Mike ", "Peter",
"Peter ", "Tom"), class = "factor"), Target1 = structure(c(8L,
4L, 6L, 5L, 2L, 1L, 3L, 7L), .Label = c("Andreas", "Cheyne",
"Frank", "John", "Mickey", "Raj", "Sarah", "Timothy"), class = "factor"),
Target2 = structure(c(4L, 3L, 1L, 5L, 2L, 1L, 1L, 1L), .Label = c("",
"Jake", "Peter", "Timothy ", "Tommy "), class = "factor"),
Parter1 = structure(c(3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"Mike ", "Timothy"), class = "factor"), Parter2 = structure(c(1L,
2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Peter"), class = "factor")), class = "data.frame", row.names = c(NA,
-8L))
我正在尝试过滤数据框,以便如果出现在此向量中的名称 x
x=c("Raj", "Timothy")
在第 2-5 列中出现超过 2 次 该行将有一个名为 flag 的全新列的变量,其中如果名称出现超过 2 次,则 flag =1,如果名称未出现,则 flag=0 2 次以上。
【问题讨论】:
-
我的理解是否正确,如果 Raj 和/或 Timothy 在该行的其他列中出现两次,或者否则为零,您希望新列为
=1?