Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a row from a data table in R [closed]

Tags:

r

I have a data table with 5778 rows and 28 columns. How do I delete ALL of the 1st row. E.g. let's say the data table had 3 rows and 4 columns and looked like this:

Row number tracking_id    3D71    3D72  3D73
    1          xxx         1       1     1
    2          yyy         2       2     2
    3          zzz         3       3     3

I want to create a data table that looks like this:

    Row number tracking_id    3D71    3D72  3D73
    1          yyy             2       2     2
    2          zzz             3       3     3

i.e. I want to delete all of row number 1 and then shift the other rows up.

I have tried datatablename[-c(1)] but this deletes the first column not the first row!

Many thanks for any help!

like image 669
lharrisl Avatar asked May 18 '16 18:05

lharrisl


People also ask

How do you delete a row from a table in R?

R provides a subset() function to delete or drop a single row and multiple rows from the DataFrame (data. frame), you can also use the notation [] and -c().

How do I remove a row from a null value in R?

To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).

How do you get rid of a row in a matrix in R?

To remove the row(s) and column(s) of a current matrix in R, we use the c() function.


2 Answers

You can do this via

dataframename = dataframename[-1,]
like image 64
Jonathan Rhein Avatar answered Oct 08 '22 04:10

Jonathan Rhein


It can be easily done with indexing the data.table/data frame as mentioned by @joni. You can also do with

datatablename <- datatablename[2:nrow(datatablename), ]

You can find more interesting stuff about data.table here.

like image 36
Fitzerbirth Avatar answered Oct 08 '22 02:10

Fitzerbirth