1.1.1 Consider a vector: x = (4,6,5,7,10,9,4,15). What is the value of: x < 7 ?

x <- c(4,6,5,7,10,9,4,15)

x < 7
## [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE

1.1.2 Consider two vectors p = (3, 5, 6, 8) & q = (3, 3, 3). What is the value of: p+q?

p <- c(3,5,6,8)  
q <- c(3,3,3)

p+q
## Warning in p + q: longer object length is not a multiple of shorter object
## length
## [1]  6  8  9 11

There is no problem at all if you get the warning message. Normally we do not sum up two vectors with different lengths duuuude

1.1.3 If;

Age <- c(22, 25, 18, 20)
Name <- c(“James”, “Mathew”, “Olivia”, “Stella”)
Gender <- c(“M”, “M”, “F”, “F”)

then what is the R-code for getting the following output;

Age Name Gender
1 22 James M
2 25 Mathew M

Age <- c(22, 25, 18, 20)  
Name <- c("James", "Mathew", "Olivia", "Stella")  
Gender <- c("M", "M", "F", "F")

data = data.frame(Age,Name,Gender)
subset(data,Gender=="M")
##   Age   Name Gender
## 1  22  James      M
## 2  25 Mathew      M

1.1.4 If z <- 0:9, then what is the output from the following R-statements:

digits <- as.character(z)
as.integer(digits)

z <- 0:9
digits <- as.character(z)  
as.integer(digits)
##  [1] 0 1 2 3 4 5 6 7 8 9

*A vector with integers basically.

1.1.5 Consider the vector x <- c(1,2,3,4). What is the value of k for:

(x+2)[(!is.na(x)) & x > 0] -> k

x <- c(1,2,3,4)

(x+2)[(!is.na(x)) & x > 0] -> k

k
## [1] 3 4 5 6

The values of k will be the values of x which are not NA and > 0.So the new values will be 3,4,5 and 6.

1.1.6 Consider the AirPassenger data set. Which statement will produce the following output?

[1] 112 118 132 129 121 135 148 148 136 119 104 118

data(AirPassengers)

AirPassengers
##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1949 112 118 132 129 121 135 148 148 136 119 104 118
## 1950 115 126 141 135 125 149 170 170 158 133 114 140
## 1951 145 150 178 163 172 178 199 199 184 162 146 166
## 1952 171 180 193 181 183 218 230 242 209 191 172 194
## 1953 196 196 236 235 229 243 264 272 237 211 180 201
## 1954 204 188 235 227 234 264 302 293 259 229 203 229
## 1955 242 233 267 269 270 315 364 347 312 274 237 278
## 1956 284 277 317 313 318 374 413 405 355 306 271 306
## 1957 315 301 356 348 355 422 465 467 404 347 305 336
## 1958 340 318 362 348 363 435 491 505 404 359 310 337
## 1959 360 342 406 396 420 472 548 559 463 407 362 405
## 1960 417 391 419 461 472 535 622 606 508 461 390 432
time(AirPassengers)
##           Jan      Feb      Mar      Apr      May      Jun      Jul
## 1949 1949.000 1949.083 1949.167 1949.250 1949.333 1949.417 1949.500
## 1950 1950.000 1950.083 1950.167 1950.250 1950.333 1950.417 1950.500
## 1951 1951.000 1951.083 1951.167 1951.250 1951.333 1951.417 1951.500
## 1952 1952.000 1952.083 1952.167 1952.250 1952.333 1952.417 1952.500
## 1953 1953.000 1953.083 1953.167 1953.250 1953.333 1953.417 1953.500
## 1954 1954.000 1954.083 1954.167 1954.250 1954.333 1954.417 1954.500
## 1955 1955.000 1955.083 1955.167 1955.250 1955.333 1955.417 1955.500
## 1956 1956.000 1956.083 1956.167 1956.250 1956.333 1956.417 1956.500
## 1957 1957.000 1957.083 1957.167 1957.250 1957.333 1957.417 1957.500
## 1958 1958.000 1958.083 1958.167 1958.250 1958.333 1958.417 1958.500
## 1959 1959.000 1959.083 1959.167 1959.250 1959.333 1959.417 1959.500
## 1960 1960.000 1960.083 1960.167 1960.250 1960.333 1960.417 1960.500
##           Aug      Sep      Oct      Nov      Dec
## 1949 1949.583 1949.667 1949.750 1949.833 1949.917
## 1950 1950.583 1950.667 1950.750 1950.833 1950.917
## 1951 1951.583 1951.667 1951.750 1951.833 1951.917
## 1952 1952.583 1952.667 1952.750 1952.833 1952.917
## 1953 1953.583 1953.667 1953.750 1953.833 1953.917
## 1954 1954.583 1954.667 1954.750 1954.833 1954.917
## 1955 1955.583 1955.667 1955.750 1955.833 1955.917
## 1956 1956.583 1956.667 1956.750 1956.833 1956.917
## 1957 1957.583 1957.667 1957.750 1957.833 1957.917
## 1958 1958.583 1958.667 1958.750 1958.833 1958.917
## 1959 1959.583 1959.667 1959.750 1959.833 1959.917
## 1960 1960.583 1960.667 1960.750 1960.833 1960.917
AirPassengers[time(AirPassengers)< 1950]
##  [1] 112 118 132 129 121 135 148 148 136 119 104 118

When you check the data, you will see that this is a time series and the requested output is the first row of the data which is the year 1949. Our statement gives us this specific row.

1.1.7 If x <- c(2, 4, 6, 8) and y <- c(TRUE, TRUE, FALSE, TRUE). What is the value of: sum(x[y])

x <- c(2, 4, 6, 8)
y <- c(TRUE, TRUE, FALSE, TRUE)
sum(x[y])
## [1] 14

1.1.8 Consider the vector: x <- c(34, 56, 55, 87, NA, 4, 77, NA, 21, NA, 39). Which R-statement will count the number of NA values in x?

x <- c(34, 56, 55, 87, NA, 4, 77, NA, 21, NA, 39)
sum(is.na(x))
## [1] 3