4

.docx

School

Indiana Wesleyan University, Marion *

*We aren’t endorsed by this school

Course

BADM-707-0

Subject

Statistics

Date

Apr 3, 2024

Type

docx

Pages

15

Uploaded by ConstableLapwingPerson2642

15.1 code: install.packages('tidyverse') install.packages('gcookbook') library(ggplot2) library(dplyr) library(gcookbook) # Two starting vectors g <- c("A", "B", "C") x <- 1:3 dat <- data.frame(g, x) dat #> g x #> 1 A 1 #> 2 B 2 #> 3 C 3 lst <- list(group = g, value = x) # A list of vectors dat <- as.data.frame(lst) data_frame(g, x) #> Warning: `data_frame()` was deprecated in tibble 1.1.0. #> Please use `tibble()` instead. #> This warning is displayed once every 8 hours. #> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was #> generated. #> # A tibble: 3 × 2 #> g x #> <chr> <int>
#> 1 A 1 #> 2 B 2 #> 3 C 3 #> # Convert the list of vectors to a tibble as_data_frame(lst) as_tibble(dat) #> # A tibble: 3 × 2 #> group value #> <chr> <int> #> 1 A 1 #> 2 B 2 #> 3 C 3 15.1 output:
15.2 code: str(ToothGrowth) #> 'data.frame': 60 obs. of 3 variables: #> $ len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ... #> $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ... #> $ dose: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ... summary(ToothGrowth) #> len supp dose #> Min. : 4.20 OJ:30 Min. :0.500 #> 1st Qu.:13.07 VC:30 1st Qu.:0.500 #> Median :19.25 Median :1.000 #> Mean :18.81 Mean :1.167 #> 3rd Qu.:25.27 3rd Qu.:2.000 #> Max. :33.90 Max. :2.000 tg <- ToothGrowth tg$supp <- as.character(tg$supp) str(tg) #> 'data.frame': 60 obs. of 3 variables: #> $ len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ... #> $ supp: chr "VC" "VC" "VC" "VC" ... #> $ dose: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ... # Print out the columns by themselves # From old data frame (factor) ToothGrowth$supp #> [1] VC VC VC VC VC VC VC VC VC VC VC VC VC VC VC VC VC VC VC VC VC VC VC VC #> [25] VC VC VC VC VC VC OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ #> [49] OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ OJ
#> Levels: OJ VC # From new data frame (character) tg$supp #> [1] "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" #> [15] "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" #> [29] "VC" "VC" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" #> [43] "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" #> [57] "OJ" "OJ" "OJ" "OJ" 15.2 output: 15.3 code: library(dplyr) ToothGrowth %>% mutate(newcol = NA) #> len supp dose newcol
#> 1 4.2 VC 0.5 NA #> 2 11.5 VC 0.5 NA #> ...<56 more rows>... #> 59 29.4 OJ 2.0 NA #> 60 23.0 OJ 2.0 NA # Since ToothGrowth has 60 rows, we must create a new vector that has 60 rows vec <- rep(c(1, 2), 30) ToothGrowth %>% mutate(newcol = vec) #> len supp dose newcol #> 1 4.2 VC 0.5 1 #> 2 11.5 VC 0.5 2 #> ...<56 more rows>... #> 59 29.4 OJ 2.0 1 #> 60 23.0 OJ 2.0 2 # Make a copy of ToothGrowth for this example ToothGrowth2 <- ToothGrowth # Assign NA's for the whole column ToothGrowth2$newcol <- NA # Assign 1 and 2, automatically repeating to fill ToothGrowth2$newcol <- c(1, 2)
15.3 output: 15.7 code: library(gcookbook) # Load gcookbook for the climate data set climate #> Source Year Anomaly1y Anomaly5y Anomaly10y Unc10y #> 1 Berkeley 1800 NA NA -0.435 0.505 #> 2 Berkeley 1801 NA NA -0.453 0.493 #> ...<495 more rows>... #> 498 CRUTEM3 2010 0.8023 NA NA NA #> 499 CRUTEM3 2011 0.6193 NA NA NA climate[climate$Source == "Berkeley" & climate$Year >= 1900 & climate$Year <= 2000, c("Year", "Anomaly10y")] #> Year Anomaly10y #> 101 1900 -0.171 #> 102 1901 -0.162 #> ...<97 more rows>...
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help