Assignment 2: R Markdown

  1. What is the second argument in the lm() function?

I typed in help(lm) and it provided me with the 2nd argument of data.

help(lm)

  1. What is the second argument in the plot.gam() function, found in the mgcv library?

To install the mgcv library I first installed the mgcv with install.packages(“mgcv”) and added it to my library via library(mgcv). Then plot.gam() with reference material from mgcv was assessable through the code help(plot.gam). I then found that the 2nd argument is residuals.

  1. What is the length of the vector created from a sequence of 3 to 27 by intervals of 0.13?

In order to find this I wrote code creating a vector sequence and asked what was the length of the vector.

The length was [1] 185

vec <- seq(from = 3, to = 27, by = 0.13) length(vec)

  1. What code subsets the first row and third column of the second matrix for the array array(seq(1:20), dim=c(2,5,2)), and what number occupies that position?

Here is the code I came up with that resulted in a value of 15.

arr <- array(seq(1:20), dim=c(2,5,2))

result <- arr[1, 3, 2] print(result) [1] 15

  1. Create a list object from 3 or more built in datasets.

In order to make this list I looked at the link and noticed a few things about libraries from earlier in class. I ended up making a list from a system library datasets ( iris, mtcars, and airquality), this was my code.

dataset_list <- list( iris = iris, mtcars = mtcars, airquality = airquality )

stdr(dataset_list)

  1. What is the mean length of major North American Rivers?

data(rivers)

mean_length <- mean(rivers)

cat(“The mean length of major North American Rivers is”, round(mean_length, 2), “miles”)

The mean length of major North American Rivers is 591.18 miles

  1. Write a built-in dataset to a CSV file (show code) and open it in Excel.

The built-in dataset I will use with be iris, and the code below shows how I have written a built-in dataset csv.

Code: data(iris)

write.csv(iris, file = “iris_dataset.csv”, row.names = FALSE)

cat(“The iris dataset has been written to ‘iris_dataset.csv’ in your working directory.”)

File:

Excel:

  1. Tabulate the type and concentration factors in the CO2 dataset in one table. Is this experiment balanced?

    data(CO2)

    print(CO2)

    Plant        Type  Treatment conc uptake

    1    Qn1      Quebec nonchilled   95   16.0

    2    Qn1      Quebec nonchilled  175   30.4

    3    Qn1      Quebec nonchilled  250   34.8

    4    Qn1      Quebec nonchilled  350   37.2

    5    Qn1      Quebec nonchilled  500   35.3

    6    Qn1      Quebec nonchilled  675   39.2

    7    Qn1      Quebec nonchilled 1000   39.7

    8    Qn2      Quebec nonchilled   95   13.6

    9    Qn2      Quebec nonchilled  175   27.3

    10   Qn2      Quebec nonchilled  250   37.1

    11   Qn2      Quebec nonchilled  350   41.8

    12   Qn2      Quebec nonchilled  500   40.6

    13   Qn2      Quebec nonchilled  675   41.4

    14   Qn2      Quebec nonchilled 1000   44.3

    15   Qn3      Quebec nonchilled   95   16.2

    16   Qn3      Quebec nonchilled  175   32.4

    17   Qn3      Quebec nonchilled  250   40.3

    18   Qn3      Quebec nonchilled  350   42.1

    19   Qn3      Quebec nonchilled  500   42.9

    20   Qn3      Quebec nonchilled  675   43.9

    21   Qn3      Quebec nonchilled 1000   45.5

    22   Qc1      Quebec    chilled   95   14.2

    23   Qc1      Quebec    chilled  175   24.1

    24   Qc1      Quebec    chilled  250   30.3

    25   Qc1      Quebec    chilled  350   34.6

    26   Qc1      Quebec    chilled  500   32.5

    27   Qc1      Quebec    chilled  675   35.4

    28   Qc1      Quebec    chilled 1000   38.7

    29   Qc2      Quebec    chilled   95    9.3

    30   Qc2      Quebec    chilled  175   27.3

    31   Qc2      Quebec    chilled  250   35.0

    32   Qc2      Quebec    chilled  350   38.8

    33   Qc2      Quebec    chilled  500   38.6

    34   Qc2      Quebec    chilled  675   37.5

    35   Qc2      Quebec    chilled 1000   42.4

    36   Qc3      Quebec    chilled   95   15.1

    37   Qc3      Quebec    chilled  175   21.0

    38   Qc3      Quebec    chilled  250   38.1

    39   Qc3      Quebec    chilled  350   34.0

    40   Qc3      Quebec    chilled  500   38.9

    41   Qc3      Quebec    chilled  675   39.6

    42   Qc3      Quebec    chilled 1000   41.4

    43   Mn1 Mississippi nonchilled   95   10.6

    44   Mn1 Mississippi nonchilled  175   19.2

    45   Mn1 Mississippi nonchilled  250   26.2

    46   Mn1 Mississippi nonchilled  350   30.0

    47   Mn1 Mississippi nonchilled  500   30.9

    48   Mn1 Mississippi nonchilled  675   32.4

    49   Mn1 Mississippi nonchilled 1000   35.5

    50   Mn2 Mississippi nonchilled   95   12.0

    51   Mn2 Mississippi nonchilled  175   22.0

    52   Mn2 Mississippi nonchilled  250   30.6

    53   Mn2 Mississippi nonchilled  350   31.8

    54   Mn2 Mississippi nonchilled  500   32.4

    55   Mn2 Mississippi nonchilled  675   31.1

    56   Mn2 Mississippi nonchilled 1000   31.5

    57   Mn3 Mississippi nonchilled   95   11.3

    58   Mn3 Mississippi nonchilled  175   19.4

    59   Mn3 Mississippi nonchilled  250   25.8

    60   Mn3 Mississippi nonchilled  350   27.9

    61   Mn3 Mississippi nonchilled  500   28.5

    62   Mn3 Mississippi nonchilled  675   28.1

    63   Mn3 Mississippi nonchilled 1000   27.8

    64   Mc1 Mississippi    chilled   95   10.5

    65   Mc1 Mississippi    chilled  175   14.9

    66   Mc1 Mississippi    chilled  250   18.1

    67   Mc1 Mississippi    chilled  350   18.9

    68   Mc1 Mississippi    chilled  500   19.5

    69   Mc1 Mississippi    chilled  675   22.2

    70   Mc1 Mississippi    chilled 1000   21.9

    71   Mc2 Mississippi    chilled   95    7.7

    72   Mc2 Mississippi    chilled  175   11.4

    73   Mc2 Mississippi    chilled  250   12.3

    74   Mc2 Mississippi    chilled  350   13.0

    75   Mc2 Mississippi    chilled  500   12.5

    76   Mc2 Mississippi    chilled  675   13.7

    77   Mc2 Mississippi    chilled 1000   14.4

    78   Mc3 Mississippi    chilled   95   10.6

    79   Mc3 Mississippi    chilled  175   18.0

    80   Mc3 Mississippi    chilled  250   17.9

    81   Mc3 Mississippi    chilled  350   17.9

    82   Mc3 Mississippi    chilled  500   17.9

    83   Mc3 Mississippi    chilled  675   18.9

    84   Mc3 Mississippi    chilled 1000   19.9

    > table_result <- table(CO2$Type, CO2$conc)

    > print(table_result)

    95 175 250 350 500 675 1000

    Quebec       6   6   6   6   6   6    6

    Mississippi  6   6   6   6   6   6    6

    is_balanced <- all(table_result == table_result[1,1])

    cat(“\nIs the experiment balanced?”, is_balanced)

    Is the experiment balanced? TRUE

    It is balanced.

  2. Write a single function to subset both spray A and records of counts \(>\) 17 from InsectSprays. How many records are there?

    subset_insect_sprays <- function(data) { subset(data, spray == “A” & count > 17) }

    data(InsectSprays)

    result <- subset_insect_sprays(InsectSprays)

    num_records <- nrow(result)

    cat(“Number of records:”, num_records)

    Number of records: 3

  3. In 2005, what was the 5th city in TX for home sales?

So txhousing is built in data with ggplt2, on the previous linked website I saved the csv to do this another way, but to get the same result. Here is the code.

txhousing <- read.csv(“txhousing.csv”)

txhousing_2005 <- txhousing[txhousing$year == 2005, ]

city_sales_2005 <- aggregate(sales ~ city, data = txhousing_2005, sum)

city_sales_2005_sorted <- city_sales_2005[order(city_sales_2005$sales, decreasing = TRUE), ]

fifth_city <- city_sales_2005_sorted$city[5]

cat(“The 5th city in TX for home sales in 2005 was:”, fifth_city)

The 5th city in TX for home sales in 2005 was: Collin County