NewUsr_stat
NewUsr_stat

Reputation: 2587

Remove rows from a data frame according to a range of row names

I would like to remove from a data.frame all rows from, for example, row number 10 plus the subsequent 4 rows, i.e. from row 10 to row 14.

The starting row for rows removing (in this case the row number 10) results from a test so that I saved the output of the test in my_test variable. Briefly I have to remove rows from my_test variable plus 4 rows. I tried:

myfile_cleaned <- my_original_file[-c(test:test+4),]   

but it does not work. Can anyone help me please?

Upvotes: 1

Views: 4908

Answers (2)

Greg Snow
Greg Snow

Reputation: 49670

This is a case where using the seq function is probably better than using : both for clarity to the reader, and correctness with precedence.

myfile_cleaned <- my_original_file[ -seq( from=test, length.out=5, by=1), ]

or

myfile_cleaned <- my_original_file[ seq( from= -test, length.out=5, by=-1), ]

Upvotes: 1

Sven Hohenstein
Sven Hohenstein

Reputation: 81753

You can use

myfile_cleaned <- my_original_file[-(test:(test + 4)), ]

Due to R's operator precedence, : is evaluated before +. Therefore you need extra parentheses.

Upvotes: 3

Related Questions