## Q1.Assign numeric value 4 to A and compute ‘(A – 4*√A)/(A+ 5*√A)’
A <- 4
(A-4*sqrt(A))/(A+5*sqrt(A))
## [1] -0.2857143
## Q2.Create sequence of numbers that count up by 4 from 0 to 48
seq(0, 48, by = 4)
## [1] 0 4 8 12 16 20 24 28 32 36 40 44 48
## Q3. Use R to compute followings
2^7/(2^7-1)
## [1] 1.007874
(1-1/2^7)^(-1)
## [1] 1.007874
exp(0.4)
## [1] 1.491825
1+0.3+0.3^2+0.3^3
## [1] 1.417
(1+0.3+0.3^2+0.3^3+0.3^5)/5
## [1] 0.283886
## Q4.Create a matrix that consists of eight country names (number of rows = 4, number of columns = 2)
# and convert the matrix into data frame
# Make sure if the matrix successfully changes into dataframe
X <- matrix(c("Turkey","Taiwan","Germany","Columbia","United States","Vietnam", "Egypt", "Canada"),nrow=4,ncol=2)
X
## [,1] [,2]
## [1,] "Turkey" "United States"
## [2,] "Taiwan" "Vietnam"
## [3,] "Germany" "Egypt"
## [4,] "Columbia" "Canada"
(df_X <- as.data.frame(X))
## V1 V2
## 1 Turkey United States
## 2 Taiwan Vietnam
## 3 Germany Egypt
## 4 Columbia Canada
is.data.frame(df_X)
## [1] TRUE
## Q5.Create two vectors that consist of four interger values for each and multiply the vectors
a <- c(2L, 4L, 8L, 10L)
b <- c(16L, 33L, 23L, 19L)
a*b
## [1] 32 132 184 190
## Q6.Create data frame including following variables
# V1 consists of four friends' names
# V2 consists of their ages
# V3 consists of their mothers' ages (just estimate / it doesn't need to be accurate)
# V4 consists of their fathers' ages (just estimate / it doesn't need to be accurate)
name <- c("Lucy", "Junga", "Mike", "Jason")
age <- c(35, 24, 55, 43)
momAge <- c(64, 55, 76, 68)
dadAge <- c(62, 57, 77, 76)
friend <- cbind(name, age, momAge, dadAge)
friend <- as.data.frame(friend)
friend$age <- as.numeric(as.character(friend$age))
friend$momAge <- as.numeric(as.character(friend$momAge))
friend$dadAge <- as.numeric(as.character(friend$dadAge))
str(friend)
## 'data.frame': 4 obs. of 4 variables:
## $ name : Factor w/ 4 levels "Jason","Junga",..: 3 2 4 1
## $ age : num 35 24 55 43
## $ momAge: num 64 55 76 68
## $ dadAge: num 62 57 77 76
## Q7. Create the column that includes the total ages for the three persons in each family (= friend + mother + father)
friend$totalAge <- friend$age + friend$momAge + friend$dadAge
## Q8. Install and load R packages ‘ggplot2’ and ‘psych’.
#install.packages("ggplot2")
#install.packages("psych")
library(ggplot2)
library(psych)
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
## Q9. Download ‘demand1.csv’ data file from Canvas and save it in a desired folder. Import the data file into R using syntax in R script.
demand1 <- read.csv(file = "C:/Users/ehk994/Desktop/Teaching/Marketing Research_Fall2020/R Session/demand1.csv", header = T)
## Q10. Download ‘adSpend.csv’ data from Canvas and save it in a desired folder. Import the data file into R using syntax in R script and cbind it with ‘demand1.csv’ and name the new data frame ‘demand2.csv’. Export the ‘demand2.csv’ to a desired folder.
adSpend <- read.csv(file = "C:/Users/ehk994/Desktop/Teaching/Marketing Research_Fall2020/R Session/adSpend.csv", header = T)
demand2 <- cbind(demand1, adSpend)
write.csv(demand2, "C:/Users/ehk994/Desktop/Teaching/Marketing Research_Fall2020/R Session/demand2.csv", sep = ",")
## Warning in write.csv(demand2, "C:/Users/ehk994/Desktop/Teaching/Marketing
## Research_Fall2020/R Session/demand2.csv", : attempt to set 'sep' ignored
## Q11.
midwest <- read.csv(file='C:/Users/ehk994/Desktop/Teaching/Marketing Research_Fall2020/R Session/midwest.csv', header = TRUE)
library(ggplot2)
midwest$inmetro <- as.factor(midwest$inmetro)
ggplot(data = midwest) +
geom_bar(
mapping = aes(x = state, fill = inmetro),
position = "dodge"
)

## Q12.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
ggplot(data = midwest, mapping = aes(x = percollege, y = percadultpoverty)) +
geom_point(mapping = aes(color = state)) +
geom_smooth(data = filter(midwest, state == "IL"),
se = FALSE
)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

## Q13.
library(dplyr)
midwest_sub <- filter(midwest, popwhite >=200000, popamerindian >=5000)
midwest_sub2 <- filter(midwest, !(popwhite < 200000 | popamerindian < 5000))
## Q14.
midwest_sub3 <- select(midwest,
PID:area,
starts_with("pop")
)
midwest_sub4 <- mutate(midwest_sub3,
poptotal2 = popwhite + popblack + popamerindian + popasian + popother,
popaverage = (popwhite + popblack + popamerindian + popasian + popother)/5
)