Skip to content

Latest commit

 

History

History
195 lines (155 loc) · 5.66 KB

File metadata and controls

195 lines (155 loc) · 5.66 KB

p8105_hw1_md4270

Manye Dong 2023-09-23

library(moderndive)
library(tidyverse)

Problem 1

# load data
data("early_january_weather")
# view the dataset
early_january_weather
## # A tibble: 358 × 15
##    origin  year month   day  hour  temp  dewp humid wind_dir wind_speed
##    <chr>  <int> <int> <int> <int> <dbl> <dbl> <dbl>    <dbl>      <dbl>
##  1 EWR     2013     1     1     1  39.0  26.1  59.4      270      10.4 
##  2 EWR     2013     1     1     2  39.0  27.0  61.6      250       8.06
##  3 EWR     2013     1     1     3  39.0  28.0  64.4      240      11.5 
##  4 EWR     2013     1     1     4  39.9  28.0  62.2      250      12.7 
##  5 EWR     2013     1     1     5  39.0  28.0  64.4      260      12.7 
##  6 EWR     2013     1     1     6  37.9  28.0  67.2      240      11.5 
##  7 EWR     2013     1     1     7  39.0  28.0  64.4      240      15.0 
##  8 EWR     2013     1     1     8  39.9  28.0  62.2      250      10.4 
##  9 EWR     2013     1     1     9  39.9  28.0  62.2      260      15.0 
## 10 EWR     2013     1     1    10  41    28.0  59.6      260      13.8 
## # ℹ 348 more rows
## # ℹ 5 more variables: wind_gust <dbl>, precip <dbl>, pressure <dbl>,
## #   visib <dbl>, time_hour <dttm>

The variables in this dataset include important variables such as year and month indicating the data is about weather from January 2013. There are also temp(temperature at the start of each hour), humid(humidity in percentage), and visib(visibility) and time_hour (the time that the temperature is recorded).

Time-related features are mostly in integer form, while the weather-related features are in double decimal form.

It has 15 columns and 358 rows. The mean temperature is 39.5821229 degree Fahrenheit.

# plot the scatter plot btw time and temp
ggplot(early_january_weather, aes(x=time_hour, y=temp, color = humid)) + geom_point()

Patterns I observed:

  • Darker blue dots represent lower humidity while lighter blue dots represent higher humidity.
  • The weather seems to be more humid with higher temperature.
  • The humidity increases in oscillation because of the different time in the day.
  • As time progresses from early January to mid-January, the weather becomes more humid.
# save the scatter plot
ggsave("humid_scatter.png")

Problem 2

# a random sample of size 10 from a standard Normal distribution
rand_samp = rnorm(10)
# a logical vector indicating whether elements of the sample are greater than 0
logical = rand_samp > 0
# a character vector of length 10
char_vec = vector("character", 10)
# a factor vector of length 10, with 3 different factor “levels”
levels = c("Level1", "Level2", "Level3")
factor_vec = factor(rep(levels, length.out=10))
# create a df from the vars above
df = tibble(random_sample = rand_samp, 
            logical_vector = logical,  
            char_vector = char_vec, 
            factor_vector = factor_vec)
df
## # A tibble: 10 × 4
##    random_sample logical_vector char_vector factor_vector
##            <dbl> <lgl>          <chr>       <fct>        
##  1       -1.66   FALSE          ""          Level1       
##  2       -1.98   FALSE          ""          Level2       
##  3       -0.196  FALSE          ""          Level3       
##  4        0.314  TRUE           ""          Level1       
##  5       -0.474  FALSE          ""          Level2       
##  6        0.201  TRUE           ""          Level3       
##  7        0.0167 TRUE           ""          Level1       
##  8       -0.236  FALSE          ""          Level2       
##  9       -0.358  FALSE          ""          Level3       
## 10       -1.20   FALSE          ""          Level1

Taking the mean of each variable:

# mean for random numbers vector
random_mean = mean(pull(df, random_sample))
random_mean
## [1] -0.5579872
# mean for logical vector
logical_mean = mean(pull(df, logical_vector))
logical_mean
## [1] 0.3
# mean for character vector
char_mean = mean(pull(df, char_vector))
## Warning in mean.default(pull(df, char_vector)): argument is not numeric or
## logical: returning NA
char_mean
## [1] NA
# mean for factor vector
factor_mean = mean(pull(df, factor_vector))
## Warning in mean.default(pull(df, factor_vector)): argument is not numeric or
## logical: returning NA
factor_mean
## [1] NA

Based on the output, taking the mean for random sample vector and logical vector worked, but those for character and factor vectors did not work.

Now, we convert the character and factor variable type using as.numeric:

# convert character vector
char_numeric = as.numeric(char_vec)
char_numeric

# convert factor vector
factor_numeric = as.numeric(factor_vec)
factor_numeric

The character vector turns all elements into “NA” and the factor vector becomes their corresponding levels (numbers from 1 to 3).

The character vectors contains all “NA” because characters/strings cannot be coalesced to numbers by simply calling a “as.numeric” function. We might have to unfactor and then convert to numeric to calculate. This helps explains why we cannot calculate the mean for the character vector.

The factor vectors contains each element’s corresponding level, which helps explain why we cannot take its mean because level has an ordinal/categorical connotation and there is no meaning in taking the mean of those levels. The elements originally were not numeric or logical.