[R] to delete lines by means of a vector

Rui Barradas ruipbarradas at sapo.pt
Tue Sep 10 17:18:05 CEST 2013


Hello,

It seems you've made a mistake and posted df1 twice,

identical(df1, df2)  # TRUE


As for your question, try negating ?%in%


MatDelete <- c(2, 4, 7, 10)

df3 <- df1[!df1$Mat %in% MatDelete, ]


Hope this helps,

Rui Barradas

Em 10-09-2013 16:03, Arnaud Michel escreveu:
> Hi
> I would like to eliminate a large number of  lines of the dataframe df1
> The lines to delete are given here by the values of Mat (ex : 2,4,7,10).
> but I have a large number (300) values of Mat
>
> dput(df1)
> structure(list(Mat = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3,
> 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7,
> 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10,
> 10, 11, 11, 11, 11, 11, 11, 11), Prenom = c("Ginette", "Ginette",
> "Ginette", "Ginette", "Ginette", "Ginette", "Nicole", "Nicole",
> "Nicole", "Nicole", "Jean", "Jean", "Jean", "Jean", "Jean", "Ginette",
> "Ginette", "Ginette", "Ginette", "Ginette", "Hélène", "Hélène",
> "Hélène", "Hélène", "Hélène", "Hélène", "Guy", "Guy", "Guy",
> "Guy", "Guy", "Guy", "Claude", "Claude", "Claude", "Claude",
> "Claude", "Claude", "Claude", "Régine", "Régine", "Régine", "Régine",
> "Régine", "Régine", "Régine", "Germain", "Germain", "Germain",
> "Germain", "Germain", "Germain", "Béatrice", "Béatrice", "Béatrice",
> "Josette", "Josette", "Josette", "Josette", "Josette", "Josette",
> "Josette"), Sexe = c("Féminin", "Féminin", "Féminin", "Féminin",
> "Féminin", "Féminin", "Féminin", "Féminin", "Féminin", "Féminin",
> "Masculin", "Masculin", "Masculin", "Masculin", "Masculin", "Féminin",
> "Féminin", "Féminin", "Féminin", "Féminin", "Féminin", "Féminin",
> "Féminin", "Féminin", "Féminin", "Féminin", "Masculin", "Masculin",
> "Masculin", "Masculin", "Masculin", "Masculin", "Masculin", "Masculin",
> "Masculin", "Masculin", "Masculin", "Masculin", "Masculin", "Féminin",
> "Féminin", "Féminin", "Féminin", "Féminin", "Féminin", "Féminin",
> "Masculin", "Masculin", "Masculin", "Masculin", "Masculin", "Masculin",
> "Féminin", "Féminin", "Féminin", "Féminin", "Féminin", "Féminin",
> "Féminin", "Féminin", "Féminin", "Féminin")), .Names = c("Mat",
> "Prenom", "Sexe"), row.names = c(NA, 62L), class = "data.frame")
>
> I would like to obtain the data frame df2
> dput(df2)
> structure(list(Mat = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3,
> 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7,
> 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10,
> 10, 11, 11, 11, 11, 11, 11, 11), Prenom = c("Ginette", "Ginette",
> "Ginette", "Ginette", "Ginette", "Ginette", "Nicole", "Nicole",
> "Nicole", "Nicole", "Jean", "Jean", "Jean", "Jean", "Jean", "Ginette",
> "Ginette", "Ginette", "Ginette", "Ginette", "Hélène", "Hélène",
> "Hélène", "Hélène", "Hélène", "Hélène", "Guy", "Guy", "Guy",
> "Guy", "Guy", "Guy", "Claude", "Claude", "Claude", "Claude",
> "Claude", "Claude", "Claude", "Régine", "Régine", "Régine", "Régine",
> "Régine", "Régine", "Régine", "Germain", "Germain", "Germain",
> "Germain", "Germain", "Germain", "Béatrice", "Béatrice", "Béatrice",
> "Josette", "Josette", "Josette", "Josette", "Josette", "Josette",
> "Josette"), Sexe = c("Féminin", "Féminin", "Féminin", "Féminin",
> "Féminin", "Féminin", "Féminin", "Féminin", "Féminin", "Féminin",
> "Masculin", "Masculin", "Masculin", "Masculin", "Masculin", "Féminin",
> "Féminin", "Féminin", "Féminin", "Féminin", "Féminin", "Féminin",
> "Féminin", "Féminin", "Féminin", "Féminin", "Masculin", "Masculin",
> "Masculin", "Masculin", "Masculin", "Masculin", "Masculin", "Masculin",
> "Masculin", "Masculin", "Masculin", "Masculin", "Masculin", "Féminin",
> "Féminin", "Féminin", "Féminin", "Féminin", "Féminin", "Féminin",
> "Masculin", "Masculin", "Masculin", "Masculin", "Masculin", "Masculin",
> "Féminin", "Féminin", "Féminin", "Féminin", "Féminin", "Féminin",
> "Féminin", "Féminin", "Féminin", "Féminin")), .Names = c("Mat",
> "Prenom", "Sexe"), row.names = c(NA, 62L), class = "data.frame")
>
> It is possible to obtain by
> df2 <- df1[df1$Mat != 2 | df1$Mat !=4 | [df1$Mat !=7 | [df1$Mat !=10,]
> But how to delete these lines when the 300 values of Mat are in the
> vector MatDelete
>
> Any ideas ?
>



More information about the R-help mailing list