Lab Exericse - Arithmetic Operators

Try the following operations in the Console of RStudio and press Enter to see the result for each line:

2 + 3
## [1] 5
2 - 3
## [1] -1
2 * 3
## [1] 6
2 / 3
## [1] 0.6666667
2 ^ 3
## [1] 8
2 %% 3
## [1] 2
2 %/% 3
## [1] 0

Lab Exercise - Relational Operators

Try the following operations in the Console of RStudio and press Enter to see the result for each line:

2 == 3
## [1] FALSE
2 != 3
## [1] TRUE
2 > 3
## [1] FALSE
2 < 3
## [1] TRUE
2 >= 3
## [1] FALSE
2 <= 3
## [1] TRUE

Lab Exercise - Logical Operators

Try the following operations in the Console of RStudio and press Enter to see the result for each line:

!TRUE
## [1] FALSE
!FALSE
## [1] TRUE
TRUE & TRUE
## [1] TRUE
TRUE & FALSE
## [1] FALSE
TRUE | FALSE
## [1] TRUE
FALSE | FALSE
## [1] FALSE

Lab Exercise - Assignment Operators

Lab Exercise - Create a vector

Try the following operations in the Console of RStudio and press Enter to see the result for each line:

c(1,3,5)
## [1] 1 3 5
c('a', 1, 3)
## [1] "a" "1" "3"
c(FALSE, 1, 3)
## [1] 0 1 3
1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(0, 10, by = 2)          
## [1]  0  2  4  6  8 10
seq(0, 10, length.out = 4)
## [1]  0.000000  3.333333  6.666667 10.000000

Lab Exercise - Create a vector

Create the following vector in three different ways in R:1, 3, 5, 7, 9

-way 1:

vec1 <- c(1, 3, 5, 7, 9)
print(vec1)
## [1] 1 3 5 7 9

-way 2:

vec2 <- seq(from = 1, to = 9, by = 2)
print(vec2)
## [1] 1 3 5 7 9

-way 3:

vec3 <- rep(c(1, 3, 5, 7, 9), times = 1)
print(vec3)
## [1] 1 3 5 7 9

Lab Exercise - Selecting vector elements

Try the following operations in the Console of RStudio and press Enter to see the result for each line:

my_vector = c('a','p','p','l','e')
my_vector[1]
## [1] "a"
my_vector[-1]
## [1] "p" "p" "l" "e"
my_vector[2:4]
## [1] "p" "p" "l"
my_vector[c(2,4)]
## [1] "p" "l"

Lab Exercise - Selecting vector elements

Try the following operations in the Console of RStudio and press Enter to see the result for each line:

x = 1:10
x[x > 5]
## [1]  6  7  8  9 10
x[x != 5]
## [1]  1  2  3  4  6  7  8  9 10
x[x <= 5]
## [1] 1 2 3 4 5

The result of x[y > 5] is error, because y is not defined

Lab Exercise

###Try the following code and see what it gives to you. Can you explain why the result is what you observed?

mean(trim = 0, na.rm = T)
## Error in mean.default(trim = 0, na.rm = T): argument "x" is missing, with no default
my_data <- c(1, 2, 2, 5, 10, NA)
mean(my_data, trim = 0, na.rm = TRUE)
## [1] 4
mean(my_data)
## [1] NA
mean(my_data, trim = 0.2)
## [1] NA
mean(my_data, trim = 0.2, na.rum = T)
## [1] NA
  1. The first line of code will result in an error because we have not defined X.
  2. The second line of code returns 4 because we calculate the mean after removing missing values.
  3. The third and fourth lines both return NA because na.rm is set to FALSE by default.
  4. The fifth line also returns NA because na.rum is a misspelling, so na.rm remains its default value, FALSE.

Lab Exercise - Basic Operations of Data Frames

Try the following commands in RStudio console. Before you execute the code, think in your mind what you expect to be the result:

my_data <- data.frame(name = c('James', 'Alice', 'Lucy'), Math_grade = c(80, 90, 100), English_grade = c(100, 90, 80))
my_data
##    name Math_grade English_grade
## 1 James         80           100
## 2 Alice         90            90
## 3  Lucy        100            80
my_data$name
## [1] "James" "Alice" "Lucy"
my_data$Math_grade
## [1]  80  90 100
my_data$English_grade
## [1] 100  90  80
my_data[1, ]
##    name Math_grade English_grade
## 1 James         80           100
my_data[2, ]
##    name Math_grade English_grade
## 2 Alice         90            90
my_data[3, 3]
## [1] 80

Lab Exercise

Statement: A study of survival of 1,225 newly diagnosed breast cancer cases finds that the average seven-year survival rates for Stage I breast cancer was 92%“.seven-year survival rates: the percentage of patients that survive seven years after diagnosis.

1. What is the population of this study? Answer: All individuals diagnosed with Stage I breast cancer.

2. What is the sample of this study? Answer: 1,225 newly diagnosed breast cancer cases

3. What is the random variable of this study? Answer: the average seven-year survival rates for Stage I breast cancer

Lab Exercises

A study shows that 71.6% of US adults are overweight. Answer the following question:

1. Under what condition is the study descriptive? Answer: We drew conclusions by collecting overweight data from all Americans.

2. Under what condition is the study inferential? Answer: We used data from a subset of Americans as a sample to draw conclusions and then predicted the overall overweight situation of the entire U.S. population.

3. Which one is more likely to be the case? Answer: The second station is more likely because it is difficult to obtain data from all Americans.

Lab Exercise

Give a real example of different variable types:

1. categorical but not ordinal *Answer:The list of deaths in a fire

2. ordinal *Answer:The severity of fire

3. discrete numeric *Answer: The number of deaths of a fire

4. continuous numeric *Answer: Temperature