Guest User

Untitled

a guest
Jul 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. ---
  2. title: "Friday meeting"
  3. author: "Duy Phan"
  4. date: "June 22, 2018"
  5. output: html_document
  6. ---
  7.  
  8. Setting working directory
  9. ```{r}
  10. setwd("D:Box Sync/5. Summer 2018/CRI Summer 2018/R/Friday meeting")
  11. ```
  12. Basic calculator. Use Crtl + enter to run selected lines
  13. ```{r}
  14. 5+5
  15. 10/5
  16. (10*2+6/80)^3
  17. ```
  18. Assign variable
  19. ```{r}
  20. a = 5
  21. b = 10
  22. a
  23. b
  24. a/b
  25. a^b
  26. a*b
  27. ```
  28. Assign letters and phrases as well, but need to use quotatation.
  29. ```{r}
  30. R = "R Rules SPSS drools!!"
  31. R
  32. ```
  33. R has several different types of data that variable can be. We will review interger, double, and factor.
  34.  
  35. Integers and double are basically the same and contain only numbers: however double accounts for variables with decimals.
  36. Using "c" means concatenate and is one way to combine elements like number in R.
  37.  
  38. Round up number in R
  39. ```{r}
  40. integer = as.integer(c(2,4,5,6))
  41. typeof(integer)
  42. double = c(4.5,6.5,9,10)
  43. typeof(double)
  44. typeof(a)
  45. typeof(R)
  46. doubleround = round(double, 0)
  47. doubleround
  48. ```
  49. Factors can be either be numbers or words. For example, a gender factor
  50. couble be maile, female, another gender identity, or 0,1,2.
  51. If gender is numbers, you will want to tell R that the gender is a factor by making it a factor using the as.factor and overwriting the variable (or making a new variable).
  52. If your variable is coded as words, you can change the reference level (i.e. the word that is alphabetically first) by using the relevel function and setting a new reference level.
  53. ```{r}
  54. genderNumber = as.factor(c(0,1,2))
  55. genderNumber
  56.  
  57. genderWords = as.factor(c("Male", "Female", "Another gender identity"))
  58. genderWords
  59.  
  60. genderWords = relevel (genderWords, ref = "Female")
  61. genderWords
  62. ```
  63. There are several different data types in R as well. We will cover vectors, matrices, data frames.
  64.  
  65. We have mostly been dealing with vectors so far. They are one row of dat. You can add them and each element will be added to the corresponding element.
  66. ```{r}
  67. vector_var1 = as.vector(c(2,3,45))
  68. vector_var1
  69.  
  70. vector_var2 = as.vector(c(5,4,3))
  71. vector_var2
  72.  
  73. vector_var12 = vector_var1 + vector_var2; vector_var12
  74. ```
  75. We can combine vectors to create matrices. You will need to specify the number of rows and columns. Given that we have two variables there should be two columns and three rows because each vectors as three data points. Vectors with differing numbers of rows cannot be combined. to subset the data you can use []
  76. ```{r}
  77. matrix_example = c(1:10); matrix_example
  78. matrix_example = matrix(matrix_example, nrow =5, ncol =2); matrix_example
  79. #Rows
  80. matrix_example[1,]
  81. #Columns
  82. matrix_example[,1]
  83. #Both
  84. matrix_example[1,2]
  85. ```
  86. The most common data type you all will be working with is data frame. Data frames need variable names.
  87. You can use the $ to get the variables, use the matrix notation, or use attach and just use the actual name.
  88.  
  89. (!) Attach function to set variable as objects.
  90. ```{r}
  91. data.frame.names = data.frame(var1 = c(1,2,3), var2 = c(4,1,6))
  92. data.frame.names
  93.  
  94. data.frame.names$var2
  95. data.frame.names[,2]
  96.  
  97. attach(data.frame.names)
  98. var1
  99. var2
  100.  
  101. ```
  102. You can also use logical operations like you would in excel
  103. ```{r}
  104. var1 > var2
  105. var1 == var2
  106. var2 >= var1
  107. ```
  108. The first thing you want to do is et the working directory. This tells R where you want to read in and store data
  109. ```{r}
  110. setwd("D:/Box Sync/5. Summer 2018/CRI Summer 2018/R/Friday meeting")
  111. write.csv(data.frame.names, "data.frame.names.csv", row.names= FALSE)
  112. data.frame.names = read.csv("data.frame.names.csv", header = TRUE, na.strings = c("na"," "))
  113. data.frame.names
  114. ```
  115. To get some summary statistics we will need some different statistical packages. This means we need to use the install.packages funcstion to install the psych and prettyR packages and then library them.
  116.  
  117. You can also get summary statistics fairly quickly
Add Comment
Please, Sign In to add comment