Guest User

Untitled

a guest
Mar 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #########Vector Data Structure##############
  2.  
  3. bag1=c(12,4,5,8,9)
  4. bag2=c(5,3,6,7)
  5. more<-bag1+bag2
  6. more
  7. #for vectorization the elements like bag1 and bag2 should have equal elements
  8. bag1=c(12,4,5,8)
  9. bag2=c(5,3,6,7)
  10. more<-bag1>=bag2
  11. more
  12. is.vector(more)
  13.  
  14.  
  15. c(1,2,3,4,5)<=3
  16. ##each and every element in c will be compare with every element in other ..this
  17. #is call as 'Broadcasting' as below.
  18.  
  19. c(1,2,3,4,5)<=c(3,7)
  20. #Note: second first verctor should be in multiple of 2nd vector other wise
  21. #it will through "longer object length is not a multiple of shorter object length"...
  22.  
  23. c(1,2,3,4,5,8)<=c(3,7)
  24.  
  25. c(1,2,3,4,5,8)<=c(0)
  26.  
  27.  
  28. draw<-c(2,3,5,6,"K")
  29. draw
  30. class(draw)
  31.  
  32. x=0:5
  33. x
  34. sum(x)
  35. class(sum)
  36. sum(x>1)
  37.  
  38. sum(x[x>1])
  39. sum
  40. x=c(4,5,2,898,'A')
  41. sort(x)
  42. sort(x,decreasing = FALSE)
  43.  
  44. sort(x,decreasing = TRUE)
  45.  
  46. x<-c(3,2,5,1,6)
  47. seq(-3:-1)
  48.  
  49. ######## Martrix DS #################
  50.  
  51.  
  52. matrix(1:6,nrow =2,ncol = 3)
  53.  
  54. #Note:lenth of metric should be no.of rows divided by no.of Columns
  55.  
  56.  
  57. matrix(1:6,nrow =9,ncol = 7)
  58.  
  59. cbind(1:3,1:3)
  60.  
  61. rbind(c(1,2,3,4,5),c(11,22,33,44,55))
  62.  
  63. cbind(c(1,2,3,4,5),c(11,22,33,44,55))
  64.  
  65. matrix(1:6,ncol = 2)
  66. matrix(1:6,byrow = TRUE,nrow =2)
  67.  
  68. m<-matrix(1:6,byrow = TRUE,nrow =2)
  69. m
  70. rbind(m,7:9)
  71. cbind(m,c(10:11))
  72.  
  73. rownames(m)<-c("Row1","Row2")
  74. colnames(m)<-c("Col1","Col2","Col3")
  75. m
  76.  
  77. ##or use dimnames attribute
  78.  
  79. m<-matrix(1:6,byrow = TRUE,nrow = 2,dimnames = list(c("Row1","Row2"),
  80. c("Col1","Col2","Col3")))
  81. m
  82.  
  83. is.matrix(m)
  84. is.vector(m)
  85.  
  86.  
  87. char<-matrix(LETTERS[1:6],nrow = 4,ncol = 3)
  88. char
  89. char<-matrix(LETTERS[1-2],nrow = 4,ncol = 3)
  90. char
  91.  
  92.  
  93. m<-matrix(sample(1:15,12),nrow = (3))
  94. m[1,3]
  95. m
  96. m[2,3]
  97. m[,3]
  98. m[3,]
  99. m[4]
  100. m[10]
Add Comment
Please, Sign In to add comment