开发者

Change the index number of a dataframe

After I'm done with some manipulation in Dataframe, I got a result dataframe. But the index are not listed properly as below.

                    MsgType/Cxr NoOfMsgs AvgElpsdTime(ms)
    161                   AM       86            30.13
    171                   CM        1              104
    18                    CO       27          1244.81
    19                    US       23          1369.61
    20                    VK        2              245
    21                    VS       11          1273.82
    112                  fqa       78          1752.22
    24                    SN       78          1752.22

I would like to get the result as like below.

                    MsgType/Cxr NoOfMsgs AvgElpsdTime(ms)
    1                   AM        86            30.13
    2                   CM         1              104
    3                    CO       27          1244.81
    4                    US       23          1369.61
    5                    VK  开发者_StackOverflow社区      2              245
    6                    VS       11          1273.82
    7                   fqa       78          1752.22
    8                    SN       78          1752.22

Please guide how I can get this ?


These are the rownames of your dataframe, which by default are 1:nrow(dfr). When you reordered the dataframe, the original rownames are also reordered. To have the rows of the new order listed sequentially, just use:

rownames(dfr) <- 1:nrow(dfr)


Or, simply

rownames(df) <- NULL

gives what you want.

> d <- data.frame(x = LETTERS[1:5], y = letters[1:5])[sample(5, 5), ]
> d
  x y
5 E e
4 D d
3 C c
2 B b
1 A a
> rownames(d) <- NULL
> d
  x y
1 E e
2 D d
3 C c
4 B b
5 A a


The index is actually the data frame row names. To change them, you can do something like:

rownames(dd) = 1:dim(dd)[1]

or

rownames(dd) = 1:nrow(dd)

Personally, I never use rownames.

In your example, I suspect that you don't need to worry about them either, since you are just renaming them 1 to n. In particular, when you subset your data frame the rownames will again be incorrect. For example,

##Simple data frame
R> dd = data.frame(a = rnorm(6))
R> dd$type = c("A", "B")
R> rownames(dd)  = 1:nrow(dd)
R> dd
        a type
1  2.1434    A
2 -1.1067    B
3  0.7451    A
4 -0.1711    B
5  1.4348    A
6 -1.3777    B

##Basic subsetting
R> dd_sub = dd[dd$type=="A",]
##Rownames are "wrong"
R> dd_sub
       a type
1 2.1434    A
3 0.7451    A
5 1.4348    A
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜