#make sure to run this first and read the csv file
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#opens and reads the csv file then renames it.
rent.US <- read_csv("price2.csv")
## Rows: 12918 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): City, Metro, County, State
## dbl (4): Population.Rank, Jan.16, May.16, Sep.16
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Exercise 1

Question 1 - Columns

#columns up down of the chr like state, city, pop, county or dates
rent.US %>% select(City)
## # A tibble: 12,918 × 1
##    City        
##    <chr>       
##  1 New York    
##  2 Los Angeles 
##  3 Chicago     
##  4 Houston     
##  5 Philadelphia
##  6 Phoenix     
##  7 Las Vegas   
##  8 San Antonio 
##  9 San Diego   
## 10 Dallas      
## # ℹ 12,908 more rows
rent.US %>% select(State)
## # A tibble: 12,918 × 1
##    State
##    <chr>
##  1 NY   
##  2 CA   
##  3 IL   
##  4 TX   
##  5 PA   
##  6 AZ   
##  7 NV   
##  8 TX   
##  9 CA   
## 10 TX   
## # ℹ 12,908 more rows
rent.US %>% select(Population.Rank)
## # A tibble: 12,918 × 1
##    Population.Rank
##              <dbl>
##  1               1
##  2               2
##  3               3
##  4               4
##  5               5
##  6               6
##  7               7
##  8               8
##  9               9
## 10              10
## # ℹ 12,908 more rows

Question 2 - Rows

# dataframe[row,colum]
rent.US[1,] 
## # A tibble: 1 × 8
##   City     Metro    County State Population.Rank Jan.16 May.16 Sep.16
##   <chr>    <chr>    <chr>  <chr>           <dbl>  <dbl>  <dbl>  <dbl>
## 1 New York New York Queens NY                  1   2335   2339   2324
rent.US[56,] 
## # A tibble: 1 × 8
##   City   Metro  County   State Population.Rank Jan.16 May.16 Sep.16
##   <chr>  <chr>  <chr>    <chr>           <dbl>  <dbl>  <dbl>  <dbl>
## 1 Aurora Denver Arapahoe CO                 56   1716   1758   1776
rent.US[31,] 
## # A tibble: 1 × 8
##   City          Metro         County  State Population.Rank Jan.16 May.16 Sep.16
##   <chr>         <chr>         <chr>   <chr>           <dbl>  <dbl>  <dbl>  <dbl>
## 1 Oklahoma City Oklahoma City Oklaho… OK                 31   1116   1116   1088

Question 3 - Columns all at once

head(rent.US[,c(1,2,3)])
## # A tibble: 6 × 3
##   City         Metro        County      
##   <chr>        <chr>        <chr>       
## 1 New York     New York     Queens      
## 2 Los Angeles  Los Angeles  Los Angeles 
## 3 Chicago      Chicago      Cook        
## 4 Houston      Houston      Harris      
## 5 Philadelphia Philadelphia Philadelphia
## 6 Phoenix      Phoenix      Maricopa
#comma in front column dataframe[empty,colum]

Question 4 - Rows all at once

head(rent.US[c(1,2,3),])
## # A tibble: 3 × 8
##   City        Metro       County      State Population.Rank Jan.16 May.16 Sep.16
##   <chr>       <chr>       <chr>       <chr>           <dbl>  <dbl>  <dbl>  <dbl>
## 1 New York    New York    Queens      NY                  1   2335   2339   2324
## 2 Los Angeles Los Angeles Los Angeles CA                  2   2596   2662   2723
## 3 Chicago     Chicago     Cook        IL                  3   1668   1686   1675
#comma behind row dataframe[row,empty]

Question 5 - Individual Row & Column

#slice specifies the number , and select choose the specifc column you want
#so this ends up being the NYC avg rent in janurary 2016
rent.US %>% slice(1) %>%
  select(Jan.16)
## # A tibble: 1 × 1
##   Jan.16
##    <dbl>
## 1   2335

Question 6 - 3 row 2 column at once

#ssame as above but now slice 3 numbers and select 2 chr/dbl
rent.US %>% slice(1, 8, 289) %>%
  select(Jan.16, Metro)
## # A tibble: 3 × 2
##   Jan.16 Metro      
##    <dbl> <chr>      
## 1   2335 New York   
## 2   1230 San Antonio
## 3   1402 Reno

Exercise 2

Question 1 - rent low to high

rent_May_ord <- rent.US %>% 
  arrange(May.16)
#now saved under new name

Question 2 - high to low pop from previous

rent_pop_rev <- rent_May_ord %>%
  arrange(desc(Population.Rank))

#glimpse(rent_pop_rev)

Exercise 3

Question 1 - top cities rent in IL

#so we need to seperate just top 3 cities,head but only top 3 head() for sept 16
rent.US %>%
  #hfilter just IL info
  filter(State == "IL") %>%
  arrange(desc(Sep.16)) %>%
  head(3) #top 3 rows 
## # A tibble: 3 × 8
##   City       Metro   County State Population.Rank Jan.16 May.16 Sep.16
##   <chr>      <chr>   <chr>  <chr>           <dbl>  <dbl>  <dbl>  <dbl>
## 1 Kenilworth Chicago Cook   IL               8571   8100   8030   8027
## 2 Winnetka   Chicago Cook   IL               3661   6293   6241   6337
## 3 Glencoe    Chicago Cook   IL               4822   5614   5539   5629

Question 2 - less 1500 west coast states

#COW replaces the IMT use " & " 
rent.COW.1500 <- rent.US %>%
  filter(State %in% c("CA", "OR", "WA") & Jan.16 <1500) %>%
  select(City, State, Jan.16)
glimpse(rent.COW.1500)
## Rows: 531
## Columns: 3
## $ City   <chr> "Fresno", "Sacramento", "Bakersfield", "Stockton", "Modesto", "…
## $ State  <chr> "CA", "CA", "CA", "CA", "CA", "CA", "WA", "WA", "WA", "OR", "CA…
## $ Jan.16 <dbl> 1197, 1400, 1349, 1274, 1242, 1330, 1006, 1360, 1487, 1339, 122…

Question 3

rent.PENN.1000 <- rent.US %>%
  filter(State == "PA" | Sep.16 <1000) %>%
  select(City,State, Sep.16)
glimpse(rent.PENN.1000)
## Rows: 3,803
## Columns: 3
## $ City   <chr> "Philadelphia", "Detroit", "Memphis", "Tulsa", "Cleveland", "Wi…
## $ State  <chr> "PA", "MI", "TN", "OK", "OH", "KS", "MO", "PA", "OH", "NY", "KY…
## $ Sep.16 <dbl> 1220, 746, 846, 983, 834, 897, 887, 1121, 788, 868, 926, 924, 9…
#contains cities in PA along with other cities rent less 1000 sept16

Exercise 4

Question 1 - New Vector

#a
animals <- c('cat', 'dog', 'cow', 'bird')
sounds <- c('meow', 'woof', 'moo', 'chirp')
#a
rbind(animals,sounds)
##         [,1]   [,2]   [,3]  [,4]   
## animals "cat"  "dog"  "cow" "bird" 
## sounds  "meow" "woof" "moo" "chirp"
#b
animals <- c('cat', 'dog', 'cow', 'bird')
sounds <- c('meow', 'woof', 'moo', 'chirp')
cbind(animals,sounds)
##      animals sounds 
## [1,] "cat"   "meow" 
## [2,] "dog"   "woof" 
## [3,] "cow"   "moo"  
## [4,] "bird"  "chirp"

Question 2 - Third Vector

numbers <- c('1', '2', '3', '4')
cbind(numbers, animals)
##      numbers animals
## [1,] "1"     "cat"  
## [2,] "2"     "dog"  
## [3,] "3"     "cow"  
## [4,] "4"     "bird"
#joined them renamed together
numbers_animals <-cbind(numbers, animals)
numbers_animals
##      numbers animals
## [1,] "1"     "cat"  
## [2,] "2"     "dog"  
## [3,] "3"     "cow"  
## [4,] "4"     "bird"

Exercise 5

Question 1 - AI Use

Used AI, Chatgtp specifically to help find short cuts to put the “%>% pipe in the code, assignment”<-“, and running the current line. Besides that AI was not used to aid in the completion of the questions.