## 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. Import built-in data set named "mtcars" in r
data("mtcars")
## Q9. Create the grouped bar plot using 'mtcars; data set that shows the frequency of two levels of 'am' for three
# level of 'cyl' (x-axis is the three level of 'cyl' and each level of 'cyl' consists of two levels of 'am',
# which is 0 or 1)
counts <- table(mtcars$am, mtcars$cyl)
barplot(counts, main="Car distribution by cyl and am",xlab="Class Level", col=c("darkblue","red"),legend
= rownames(counts), beside=TRUE)

## Q10. Subset 'mtcars' thac consists of the first three columns and name it 'sub_mtcars'
sub_mtcars = mtcars[c(1:3)]
## Q11. Subset 'mtcars' based on following condition
# Select all rows that have a value of mpg greater than or equal to 25 or mpg less than 15 where am = 1
sub_mtcars2 <- mtcars[ which(mtcars$am=='1'
& mtcars$mpg >= 25 | mtcars$mpg < 15), ]