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??
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().
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With