Arrays array matrix list data frame Dataframe

Source: Internet
Author: User
Tags sin

Transferred from: http://blog.csdn.net/u011253874/article/details/43115447

  1. <span style= "FONT-SIZE:14PX;" > #R语言备忘录三 #
  2. #数组array和矩阵matrix, list, data frame Dataframe
  3. #数组
  4. #数组的重要属性就是dim, Number of dimensions
  5. Matrix of #得到4
  6. Z <-1:12
  7. Dim (z) <-C (3,4)
  8. Z
  9. #构建数组
  10. X <-Array (1:20, Dim = C (4,5))
  11. #三维
  12. Y <-Array (1:18, Dim = C (2,3,3))
  13. #数组下标
  14. Y[1, 2, 3]
  15. #数组的广义转置, dimensions change, turn 2 dimensions into 1 dimensions, turn 3 dimensions into 2 dimensions, 1 dimensions into 3 dimensions, i.e. d[i,j,k] = C[j,k,i]
  16. C <-Array (1:24, Dim = C (2,3,4))
  17. D <-Aperm (A, C (2,3,1))
  18. #apply用于数组固定某一维度不变, perform calculations
  19. Apply (A, 1, sum)
  20. #矩阵
  21. #产生矩阵
  22. A <-Matrix (1:15, nrow=3, ncol=5, Byrow=true)
  23. B <-Matrix (3:17, nrow=5, ncol=3, Byrow=true)
  24. #求方阵行列式的值
  25. Det (Matrix (1:4, Ncol = 2))
  26. #内积
  27. #矩阵的内积
  28. A%*% B
  29. #也可以使用crossprod函数
  30. Crossprod (A, B)
  31. Crossprod (A)
  32. #向量的外积, also known as cross-product
  33. X <-1:5
  34. Y <-2*1:5
  35. X%o% y
  36. #也可以使用tcrossprod函数, outer
  37. Outer (x, y)
  38. Tcrossprod (x)
  39. #产生对角阵
  40. #如果变量是一个向量, a diagonal array with a vector as a diagonal element
  41. V <-C (1, 3, 5)
  42. Diag (v)
  43. #如果变量是一个矩阵, we take the diagonal elements of the matrix as diagonal elements
  44. Diag (A)
  45. #解线性方程Ax =b,
  46. b <-Matrix (C (1,1,1), nrow = 3, Byrow = TRUE)
  47. B <-Matrix (1:9, nrow = 3, Byrow = TRUE)
  48. Solve (B,B)
  49. #求矩阵的逆
  50. Solve (B)
  51. #ev $values is a characteristic root, and ev$vectors is a matrix of eigenvectors.
  52. SM <-Crossprod (A, a)
  53. EV <-Eigen (SM)
  54. #奇异解, SVD$D returns the singular value of matrix A, Svd$u is the orthogonal array u,svd$v corresponding to the orthogonal array V, a = UDVT
  55. Svda <-SVD (A)
  56. #把矩阵拉成向量
  57. As.vector (A)
  58. #数组或矩阵的维的名字
  59. X <-Matrix (1:6, Ncol = 2,
  60. Dimnames = List (c ("One", "B", "three"), C ("First", "Second")),
  61. Byrow = T)
  62. #亦或是
  63. Dimnames (A) <-list (C ("One", "B", "three"), C ("First", "Second"))
  64. Colnames (A) <-C ("First", "Second")
  65. Rownames (A) <-C ("One", "B", "three")
  66. #列表
  67. #构建列表
  68. Lst <-list (name= "Fred", wife= "Mary", No.children=3, Child.ages=c (4,7,9))
  69. #列表元素, list name [[Subscript]]
  70. LST[[2]]
  71. LST[[4]][2]
  72. #也可以使用名字代替下标
  73. lst[["Name"]
  74. Lst$name
  75. #修改列表
  76. Lst$name <-C ("John", "Tom")
  77. #删除列表某项
  78. Lst$name <-NULL
  79. #连接几个列表
  80. List. ABC <-C (list. A, List. B, List. C
  81. #数据框
  82. #data. Frame, usage and list are the same, if the list of ingredients satisfies the data frame, you can also use the As.data.frame conversion
  83. Df<-data.frame (
  84. Name=c ("Alice", "Becka", "James", "Jeffrey", "John"), Sex=c ("F", "F", "M", "M", "M"),
  85. Age=c (13, 13, 12, 13, 12),
  86. Height=c (56.5, 65.3, 57.3, 62.5, 59.0)
  87. Weight=c (84.0, 98.0, 83.0, 84.0, 99.5)
  88. )
  89. Df
  90. #矩阵可以通过data. FRAME (X) conversion
  91. #取数据框的一部分
  92. Df[1:2, 3:5]
  93. df[["Height"]
  94. Df$weight
  95. #命名
  96. Names (DF)
  97. Rownames (DF)
  98. The #attch () function calls the variables in the data frame into memory to facilitate the invocation of data
  99. Attach (DF)
  100. #取消连接
  101. Detach (DF)
  102. #调用edit进行编辑数据
  103. Edit (DF)
  104. #因子factor
  105. Sex <-C ("M", "M", "F", "F")
  106. SEXF <-factor (Sex)
  107. #因子水平
  108. Sex_level <-levels (SEXF)
  109. #用table统计各类数据的频数
  110. Sex_tab <-table (SEXF)
  111. #用gl () Production factor
  112. #gl (n, k, length = n * k, labels = 1:n, ordered = FALSE)
  113. </span>

、、、、、、、、、、、、、、、、、、、、、、、、、、、

  1. #R语言学习备忘录一 #
  2. #向量运算 #
  3. x <-C (1,2,3,6)
  4. #不小于x的最小整数
  5. Ceiling (x)
  6. #不大于x的最大整数
  7. Floor (x)
  8. #向0方向截取的x中的整数
  9. Trunc (x)
  10. #将x舍入为指定位的小数
  11. Round (x,digits=2)
  12. #将x舍入指定的有效数字位数
  13. Signif (x,digits=2)
  14. #三角函数
  15. COS (x)
  16. Sin (x)
  17. Tan (x)
  18. ACOs (x)
  19. ASIN (x)
  20. #分位数, such as 25% and 50% of the number of digits
  21. Quantile (X,c (. 25,.5))
  22. #求值域
  23. Range (x)
  24. #求乘积函数
  25. Prod (x)
  26. #滞后差分
  27. diff (X)
  28. #求最大值和最小值对应的位置
  29. Which.min (x)
  30. Which.max (x)
  31. #数据标准化
  32. X<-c (123,232,212,232,120,273)
  33. Mydata<-scale (x)
  34. MyData
  35. #正则表达式匹配
  36. Str<-c ("A", "a", "B", "C")
  37. grep ("A", Str,fixed=true)
  38. #字符
  39. #分割符strsplit
  40. Strsplit ("ABCDE", "" ")
  41. #连接字符paste
  42. Paste ("x", 1:10,sep= "")
  43. Paste ("x", 1:5,sep= "T")
  44. Paste ("Today is", date ())
  45. #大写转换toupper
  46. ToUpper ("Abcef")
  47. #小写转换tolower
  48. ToLower ("ABC")
  49. #计算字符数量 nchar
  50. X<-c ("AB", "Deew", "James")
  51. NCHAR (x)
  52. NCHAR (x[2])
  53. #提取或替换一个数值, and the Excel mid function is almost substr
  54. x<-"ABCDEFG"
  55. substr (x,2,5) #2到5个元素
  56. substr (x,2,5) <-"1111111"
  57. X
  58. #注意两种等差数列的差别
  59. 1:n-1
  60. 1: (n-1)
  61. #重复函数rep
  62. Y<-rep (1:5,2)
  63. Y
  64. #等间隔函数
  65. Seq ( -5, 5, by=.2)
  66. Seq (length=51, from=-5, by=.2)
  67. #上下两种方式效果一样
  68. #求行均值
  69. Apply (Mydata,1,mean)
  70. #求列均值
  71. Apply (Mydata,2,mean)
  72. #逻辑变量
  73. #判断一个逻辑向量是否都为真的函数是all
  74. All (c (1,2,3,4,5,6) >3)
  75. #判断一个逻辑向量是否有为真的函数any
  76. Any (c (1,2,3,4,5,6) >3)
  77. #缺失数据
  78. #NA表示数据缺省或缺失
  79. Z <-C (1:3, NA)
  80. Z
  81. A <-is.na (z)
  82. #修改缺失数据
  83. Z[is.na (z)] <-0
  84. #is. Nan () to determine whether the data is accurate, the INF is also a non-accurate
  85. x <-C (0/1, 0/0, 1/0, NA)
  86. Is.nan (x)
  87. Is.finite (x)
  88. Is.infinite (x)
  89. Is.na (x)
  90. #复数向量
  91. #复数z =x+isin (x)
  92. x <-seq (-pi, pi, by = PI/10)
  93. Y <-sin (x)
  94. Z <-Complex (re = x, Im = y)
  95. Plot (z)
  96. Lins (z)
  97. #向量的下标运算
  98. x <-C (1,4,7)
  99. X[c (2,3)]
  100. #修改元素值
  101. X[c (1,3)] <-C (22, 33)
  102. #逻辑向量
  103. x <-C (1,4,7)
  104. X < 5
  105. X[X<5]
  106. #分段函数
  107. Y <-Numeric (length (x))
  108. Y[X<0] <-1-x[x<0]
  109. Y[x>=0] <-1-x[x>=0]
  110. #即y =1-x,x<0; Y=1+x,x>=0
  111. #下标的负整数运算表示的是删除
  112. V <-10:20
  113. v[-(1:5)]
  114. #取字符串为下标
  115. Ages <-C (li=33, zhang=29, liu=18)
  116. ages["Zhang"]
  117. #给向量赋予名字
  118. Fruit <-x (5, 10, 1, 29)
  119. Names (fruit) <-C ("Orange", "banana", "apple", "peach")

Arrays array matrix list data frame Dataframe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.