在下面的data.frame中,我有3个变量。 id(A1至A10); var A到E和值。我想根据这些条件删除特定的行
对于每个变量var
(A,B,C,D或E);
如果
id == A5
和如果
value < 5
然后
删除此特定变量的所有行(10行)。
我将很感激如何做到这一点。
> dput(df)
structure(list(id = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 2L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 2L, 1L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 2L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 2L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 2L), .Label = c("A1",
"A10", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"), class = "factor"),
var = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("A",
"B", "C", "D", "E"), class = "factor"), value = c(2, 4, 7,
8, 8, 6, 3, 8, 4, 2, 3, 5, 4, 3, 2, 7, 4, 2, 1, 22, 54, 21,
36, 52, 14, 75, 12, 15, 24, 3, 2, 4, 5, 7, 9, 21, 22, 12,
12, 12, 0.3, 0.4, 0.9, 2, 3, 2, 0.5, 0.2, 0.4, 0.7)), .Names = c("id",
"var", "value"), class = "data.frame", row.names = c(NA, -50L
))
答案 0 :(得分:5)
我们可以使用dplyr
和group_by
使用filter
执行此操作,我们会移除var
any
次id
的组value
as&#34; A5&#34;和library(dplyr)
df %>%
group_by(var) %>%
filter(!any(id == 'A5' & value < 5))
小于5。
@Override
public void success(final Result<TwitterSession> twitterSessionResult) {
twitterName = twitterSessionResult.data.getUserName();
prefs.edit().putString(TWITTER_NAME_PREF, twitterName).apply();
mTwitterAuthClient.requestEmail(twitterSessionResult.data, new Callback<String>() {
@Override
public void success(Result<String> stringResult) {
twitterEmail = stringResult.data;
prefs.edit().putString(TWITTER_EMAIL_PREF, twitterEmail).apply();
mFragment.twitterInfo(twitterName,
twitterEmail);
finishLogIn(true, null);
}
答案 1 :(得分:1)
我们可以使用base R
df[with(df, ave(!(id == 'A5' & value < 5), var, FUN = any)),]
或使用data.table
library(data.table)
setDT(df)[df[, .I[any(!(id == 'A5' & value < 5))], var]$V1]
答案 2 :(得分:0)
你总能做到
df %>% filter((id != A5) | (value >= 5))