Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 1.62 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Change the index number of a dataframe
  2. MsgType/Cxr NoOfMsgs AvgElpsdTime(ms)
  3.     161                   AM       86            30.13
  4.     171                   CM        1              104
  5.     18                    CO       27          1244.81
  6.     19                    US       23          1369.61
  7.     20                    VK        2              245
  8.     21                    VS       11          1273.82
  9.     112                  fqa       78          1752.22
  10.     24                    SN       78          1752.22
  11.        
  12. MsgType/Cxr NoOfMsgs AvgElpsdTime(ms)
  13.     1                   AM        86            30.13
  14.     2                   CM         1              104
  15.     3                    CO       27          1244.81
  16.     4                    US       23          1369.61
  17.     5                    VK        2              245
  18.     6                    VS       11          1273.82
  19.     7                   fqa       78          1752.22
  20.     8                    SN       78          1752.22
  21.        
  22. rownames(dfr) <- 1:nrow(dfr)
  23.        
  24. rownames(dd) = 1:dim(dd)[1]
  25.        
  26. rownames(dd) = 1:nrow(dd)
  27.        
  28. ##Simple data frame
  29. R> dd = data.frame(a = rnorm(6))
  30. R> dd$type = c("A", "B")
  31. R> rownames(dd)  = 1:nrow(dd)
  32. R> dd
  33.         a type
  34. 1  2.1434    A
  35. 2 -1.1067    B
  36. 3  0.7451    A
  37. 4 -0.1711    B
  38. 5  1.4348    A
  39. 6 -1.3777    B
  40.  
  41. ##Basic subsetting
  42. R> dd_sub = dd[dd$type=="A",]
  43. ##Rownames are "wrong"
  44. R> dd_sub
  45.        a type
  46. 1 2.1434    A
  47. 3 0.7451    A
  48. 5 1.4348    A
  49.        
  50. rownames(df) <- NULL
  51.        
  52. > d <- data.frame(x = LETTERS[1:5], y = letters[1:5])[sample(5, 5), ]
  53. > d
  54.   x y
  55. 5 E e
  56. 4 D d
  57. 3 C c
  58. 2 B b
  59. 1 A a
  60. > rownames(d) <- NULL
  61. > d
  62.   x y
  63. 1 E e
  64. 2 D d
  65. 3 C c
  66. 4 B b
  67. 5 A a