Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. ###############################################################################
  2. #
  3. # INTRODUCTION TO R
  4. #
  5. ###############################################################################
  6.  
  7. # calculator
  8. (50 + 1.45)/12.5
  9.  
  10.  
  11. # assignment operators
  12. x = 945
  13. y <- sin(0.47)^2 * sqrt(5)
  14. y^2 -> z
  15.  
  16.  
  17. # to inspect the value of a variable simply type its name
  18. x
  19. y
  20. z
  21.  
  22.  
  23. # listing and deleting objects
  24. ls()
  25. rm(y)
  26. rm(x,z)
  27.  
  28. # remove (almost) everything in the working environment
  29. rm(list=ls())
  30.  
  31.  
  32. #
  33. # Vectors (the most basic data objects in R)
  34. #
  35.  
  36. # creating vectors
  37. v <- c(14,7,23.5,76.2)
  38. v
  39.  
  40. # generating a regular sequence of numbers
  41. v <- 1:10
  42. v
  43.  
  44. v <- seq(from=5, to=10, by=2)
  45. v
  46.  
  47. w <- rep(v, times = 2)
  48. w
  49.  
  50.  
  51. # scalars are vectors with a single element
  52. w <- 45.0
  53.  
  54. # vectors can be created using other vectors
  55. z <- c(v, 2.5, w)
  56. z
  57.  
  58.  
  59.  
  60.  
  61. #
  62. # Useful functions
  63. #
  64.  
  65. v <- c(8, 4, 2, 3, 6, 9, 1)
  66.  
  67. length(v)
  68. max(v)
  69. min(v)
  70. which.min(v)
  71. sum(v)
  72. mean(v)
  73. sd(v)
  74. rev(v)
  75. sort(v)
  76. sort(v, decreasing=T)
  77. order(v)
  78.  
  79.  
  80. # types of vectors
  81. mode(v)
  82.  
  83. # logical vector - has logical constants as elements
  84. b <- c(TRUE, FALSE, F, T)
  85. b
  86. mode(b)
  87.  
  88. x <- 5 > 3
  89. x
  90. mode(x)
  91.  
  92.  
  93. # string vector - has strings as elements
  94. s <- c("character", "logical", "numeric", "complex")
  95. mode(s)
  96.  
  97.  
  98. # type coercion (all elements must be of the same type)
  99. x <- c(F, T, 34.56, 'aaa')
  100. x
  101.  
  102.  
  103. #
  104. # Vectorization
  105. #
  106.  
  107. # vector arithmetic (operations are performed element-wise)
  108. v1 <- c(10,20,30,40)
  109. v2 <- 1:4
  110. v1 + v2
  111. v1 * v2
  112.  
  113.  
  114. # functions operate directly on each element of a vector
  115. v1^2
  116. sqrt(v1)
  117. exp(v1)
  118. log2(v1)
  119.  
  120. # the recycling rule (if lengths are different the elements of the shorter vector are repeated)
  121. v1 * 10
  122. v1 + 1
  123. v1 + c(100, 200)
  124.  
  125.  
  126.  
  127. #
  128. # Indexing
  129. #
  130.  
  131. x <- c(-10,20,-30,40,-50,60,-70,80)
  132. x
  133.  
  134.  
  135. # individual elements can be addressed using an integer index vector
  136. # (indexing starts with 1)
  137. x[3]
  138. x[c(1,4,5)]
  139. x[1:3]
  140. x[]
  141.  
  142.  
  143. # negative integer indices address all elements but those stated
  144. x[-1]
  145. x[-c(4,6)]
  146. x[-(1:3)]
  147.  
  148.  
  149. # vector elements can be addressed using logical vectors
  150. # (elements corresponding to constants TRUE are selected)
  151.  
  152. # logical vector
  153. x > 0
  154.  
  155. # logical vector indexing
  156. x[x>0]
  157. x[x <= -20 | x > 50]
  158. x[x > 40 & x < 100]
  159.  
  160. # equality operator is ==
  161. # inequality operator is !=
  162.  
  163.  
  164. # the which() function returns indices corresponding to constants TRUE
  165. which(x > 0)
  166.  
  167.  
  168.  
  169. # character string index vector
  170. point <- c(4.7, 3.6, 2.5)
  171. names(point) <- c('x', 'y', 'z')
  172. point
  173.  
  174. point['x']
  175. point[c('x','z')]
  176.  
  177. # empty indices
  178. point[] <- 0
  179. point
  180.  
  181. # not the same as
  182. point <- 0
  183. point
  184.  
  185.  
  186. #
  187. # Vector editing
  188. #
  189.  
  190. x <- c("a", "b", "c", "d")
  191.  
  192. # replacing an element
  193. x[2] <- "BBBBB"
  194. x
  195.  
  196. x[c(1,3)] <- c("AAAAA", "CCCCC")
  197. x
  198.  
  199. # adding new element
  200. x[length(x)+1] = "EEEEE"
  201. x
  202.  
  203. # what happens if we do not define all elements in the vector?
  204. x[10] <- "FFFFF"
  205. x
  206.  
  207. # which elements are not defined
  208. is.na(x)
  209.  
  210.  
  211. # removing elements
  212. x <- x[-c(1,3)]
  213. x
  214.  
  215. x <- c(x[2],x[3])
  216. x
  217.  
  218.  
  219.  
  220.  
  221. #
  222. # Factors
  223. #
  224.  
  225. gender <- c("f","m","m","m","f","m","f")
  226. gender
  227.  
  228. # factors are useful when modelling nominal variables
  229. gender <- factor(gender)
  230. gender
  231.  
  232. # argument "levels" defines all possible elements' values
  233. dir <- factor(c('left','left','up'), levels = c('left','right','up','down'))
  234. dir
  235.  
  236. # all possible elements' values
  237. levels(dir)
  238.  
  239. # if no match is found
  240. dir[1] <- "diagonal"
  241. dir
  242.  
  243. # valid assignment
  244. dir[1] <- "down"
  245. dir
  246.  
  247. # frequency tables for factors
  248. table(gender)
  249. table(dir)
  250.  
  251.  
  252.  
  253. #
  254. # Lists (an ordered collection of objects - components)
  255. #
  256.  
  257. # creating a list
  258. student <- list(id=12345,name="Marko",marks=c(10,9,10,9,8,10))
  259. student
  260.  
  261. # extracting elements of a list (using named components)
  262. student$id
  263. student$name
  264. student$marks
  265.  
  266. # extracting elements of a list (using indexing)
  267. student[[1]]
  268. student[[2]]
  269. student[[3]]
  270.  
  271. # extending lists
  272. student$parents <- c("Ana", "Tomaz")
  273. student
  274.  
  275.  
  276. #
  277. # Data frames
  278. #
  279.  
  280. # creating a data frame
  281. height <- c(179, 185, 183, 172, 174, 185, 193, 169, 173, 168)
  282. weight <- c(95, 89, 70, 80, 92, 86, 100, 63, 72, 70)
  283. gender <- factor(c("f","m","m","m","f","m","f","f","m","f"))
  284. student <- c(T, T, F, F, T, T, F, F, F, T)
  285.  
  286. df <- data.frame(gender, height, weight, student)
  287. df
  288.  
  289. # some important functions
  290. summary(df)
  291. names(df)
  292. nrow(df)
  293. ncol(df)
  294.  
  295. # accessing elements of data frames
  296. df[5,]
  297. df[1:5,]
  298. df[,1]
  299. df[,c(1,3,4)]
  300. df[1,3]
  301. df[1,-3]
  302.  
  303. df$height
  304.  
  305. df[df$height < 180,]
  306. df[df$gender == "m",]
  307.  
  308.  
  309. # adding columns to a data frame
  310. df <- cbind(df, age = c(20, 21, 30, 25, 27, 19, 24, 27, 28, 24))
  311. df
  312.  
  313. df$name = c("Joan","Tom","John","Mike","Anna","Bill","Tina","Beth","Steve","Kim")
  314. df
  315.  
  316. summary(df)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement