How do I exclude a set of specific lines, listed in a vector, from a data frame in R?

Asked

Viewed 21,039 times

3

Hello, I have a data.frame with thousands of lines and a vector with the lines I need this data.frame, as I do to select only the lines of the list in a separate object?

3 answers

2

Use the dplyr():

Import packages

library(dplyr)

Sample Data Frame

df <- data.frame(a = 1:5, b = 1:5)

removing line 1 and 2, for example:

df <- df %>% slice(3:n())

2

Suppose you have a data.frame this way:

 df <- data.frame(a = 1:10, b = 1:10)

And that you want to select only the lines 1,5 and 8 that are in the vector:

linhas <- c(1,5,8)

Then you can select them and save to a new object as follows:

df.novo <- df[linhas,]
df.novo
  a b
1 1 1
5 5 5
8 8 8

If you wanted to delete lines 1.5 and 8 you could do as follows:

df.novo <- df[-linhas, ]
df.novo
    a  b
2   2  2
3   3  3
4   4  4
6   6  6
7   7  7
9   9  9
10 10 10

0

For a situation where more manpower is needed for data handling, and expected good yield, use of the package data.table can be of great help.

I leave here a brief example using the Toy date by @Daniel Falbel.

library(data.table)
df <- data.frame(a = 1:10, b = 1:10)
df <- data.table::data.table(df)

linhas <- c(1,5,8)

df <- df[,.SD[-linhas]]
df
> df
    a  b
1:  2  2
2:  3  3
3:  4  4
4:  6  6
5:  7  7
6:  9  9
7: 10 10

Browser other questions tagged

You are not signed in. Login or sign up in order to post.