Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

r - 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        2              245
    6                    VS       11          1273.82
    7                   fqa       78          1752.22
    8                    SN       78          1752.22

Please guide how I can get this ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...