Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically shrink down row numbers in R data frame when removing rows in R

Tags:

dataframe

r

row

I'm having a difficulty properly shrinking down the row numbers in a data frame.

I have a data set named "mydata" which I imported from a text file using R. The data frame has about 200 rows with 10 columns.

I removed the row number 3, 7, 9, 199 by using:

mydata <- mydata[-c(3, 7, 9, 199),]

When I run this command, the row 3,7,9,199 are gone from the list but the row number doesn't automatically shrink down to 196, but stays at 200. I feel like somehow these row numbers are attached to each "row" as part of the dataframe?

How do I fix this problem?

What puzzles me even more is that when I import the textfile using R Studio, I don't have any problem. (I see 196 when I run the above command). But when using R, I can't change the row number in a dataframe that matches the actual number of rows in the list.

Can anyone please tell me how to fix this??

like image 697
Pirate Avatar asked Sep 11 '12 01:09

Pirate


People also ask

How do I remove rows from numbers in R?

Use -c() with the row id you wanted to delete, Using this we can delete multiple rows at a time from the R data frame. Here row index numbers are specified inside vector c().

How do I reorder row numbers in R?

The dplyr function arrange() can be used to reorder (or sort) rows by one or more variables. Instead of using the function desc(), you can prepend the sorting variable by a minus sign to indicate descending order, as follow. If the data contain missing values, they will always come at the end.


2 Answers

You can simply do:

rownames(mydata) <- NULL

after performing the subsetting.

For example:

> mydata = data.frame(a=1:10, b=11:20)
> mydata = mydata[-c(6, 8), ]
> mydata
    a  b
1   1 11
2   2 12
3   3 13
4   4 14
5   5 15
7   7 17
9   9 19
10 10 20
> rownames(mydata) <- NULL
> mydata
   a  b
1  1 11
2  2 12
3  3 13
4  4 14
5  5 15
6  7 17
7  9 19
8 10 20
like image 71
David Robinson Avatar answered Sep 21 '22 22:09

David Robinson


You could also use the data.table package which does not store row.names in the same way (see the data.table intro, instead it will print with the row number.

See the section on keys for how data.table works with row names and keys

data.table inherits from data.frame, so a data.table is a data.frame if functions and pacakges accept only data.frames.

eg

library(data.table)

mydata <- data.table(mydata)
mydata
##      a  b
##  1:  1 11
##  2:  2 12
##  3:  3 13
##  4:  4 14
##  5:  5 15
##  6:  6 16
##  7:  7 17
##  8:  8 18
##  9:  9 19
## 10: 10 20

mydata = mydata[-c(6, 8), ]
mydata
##     a  b
## 1:  1 11
## 2:  2 12
## 3:  3 13
## 4:  4 14
## 5:  5 15
## 6:  7 17
## 7:  9 19
## 8: 10 20
like image 20
mnel Avatar answered Sep 23 '22 22:09

mnel