Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- title: "Friday meeting"
- author: "Duy Phan"
- date: "June 22, 2018"
- output: html_document
- ---
- Setting working directory
- ```{r}
- setwd("D:Box Sync/5. Summer 2018/CRI Summer 2018/R/Friday meeting")
- ```
- Basic calculator. Use Crtl + enter to run selected lines
- ```{r}
- 5+5
- 10/5
- (10*2+6/80)^3
- ```
- Assign variable
- ```{r}
- a = 5
- b = 10
- a
- b
- a/b
- a^b
- a*b
- ```
- Assign letters and phrases as well, but need to use quotatation.
- ```{r}
- R = "R Rules SPSS drools!!"
- R
- ```
- R has several different types of data that variable can be. We will review interger, double, and factor.
- Integers and double are basically the same and contain only numbers: however double accounts for variables with decimals.
- Using "c" means concatenate and is one way to combine elements like number in R.
- Round up number in R
- ```{r}
- integer = as.integer(c(2,4,5,6))
- typeof(integer)
- double = c(4.5,6.5,9,10)
- typeof(double)
- typeof(a)
- typeof(R)
- doubleround = round(double, 0)
- doubleround
- ```
- Factors can be either be numbers or words. For example, a gender factor
- couble be maile, female, another gender identity, or 0,1,2.
- 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).
- 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.
- ```{r}
- genderNumber = as.factor(c(0,1,2))
- genderNumber
- genderWords = as.factor(c("Male", "Female", "Another gender identity"))
- genderWords
- genderWords = relevel (genderWords, ref = "Female")
- genderWords
- ```
- There are several different data types in R as well. We will cover vectors, matrices, data frames.
- 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.
- ```{r}
- vector_var1 = as.vector(c(2,3,45))
- vector_var1
- vector_var2 = as.vector(c(5,4,3))
- vector_var2
- vector_var12 = vector_var1 + vector_var2; vector_var12
- ```
- 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 []
- ```{r}
- matrix_example = c(1:10); matrix_example
- matrix_example = matrix(matrix_example, nrow =5, ncol =2); matrix_example
- #Rows
- matrix_example[1,]
- #Columns
- matrix_example[,1]
- #Both
- matrix_example[1,2]
- ```
- The most common data type you all will be working with is data frame. Data frames need variable names.
- You can use the $ to get the variables, use the matrix notation, or use attach and just use the actual name.
- (!) Attach function to set variable as objects.
- ```{r}
- data.frame.names = data.frame(var1 = c(1,2,3), var2 = c(4,1,6))
- data.frame.names
- data.frame.names$var2
- data.frame.names[,2]
- attach(data.frame.names)
- var1
- var2
- ```
- You can also use logical operations like you would in excel
- ```{r}
- var1 > var2
- var1 == var2
- var2 >= var1
- ```
- The first thing you want to do is et the working directory. This tells R where you want to read in and store data
- ```{r}
- setwd("D:/Box Sync/5. Summer 2018/CRI Summer 2018/R/Friday meeting")
- write.csv(data.frame.names, "data.frame.names.csv", row.names= FALSE)
- data.frame.names = read.csv("data.frame.names.csv", header = TRUE, na.strings = c("na"," "))
- data.frame.names
- ```
- 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.
- You can also get summary statistics fairly quickly
Add Comment
Please, Sign In to add comment