-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp8105_hw1_md4270.Rmd
More file actions
113 lines (95 loc) · 3.61 KB
/
Copy pathp8105_hw1_md4270.Rmd
File metadata and controls
113 lines (95 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
title: "p8105_hw1_md4270"
author: "Manye Dong"
date: "2023-09-23"
output: github_document
---
```{r message=FALSE, results='hide'}
library(moderndive)
library(tidyverse)
```
## Problem 1
```{r}
# load data
data("early_january_weather")
```
```{r}
# view the dataset
early_january_weather
```
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 `r mean(early_january_weather$temp)` degree Fahrenheit.
```{r}
# 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.
```{r message=FALSE, results='hide'}
# save the scatter plot
ggsave("humid_scatter.png")
```
## Problem 2
```{r random sample vec}
# a random sample of size 10 from a standard Normal distribution
rand_samp = rnorm(10)
```
```{r logical vec}
# a logical vector indicating whether elements of the sample are greater than 0
logical = rand_samp > 0
```
```{r character vec}
# a character vector of length 10
char_vec = vector("character", 10)
```
```{r factor vec}
# a factor vector of length 10, with 3 different factor “levels”
levels = c("Level1", "Level2", "Level3")
factor_vec = factor(rep(levels, length.out=10))
```
```{r create a df from them}
# 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
```
Taking the mean of each variable:
```{r}
# mean for random numbers vector
random_mean = mean(pull(df, random_sample))
random_mean
```
```{r}
# mean for logical vector
logical_mean = mean(pull(df, logical_vector))
logical_mean
```
```{r}
# mean for character vector
char_mean = mean(pull(df, char_vector))
char_mean
```
```{r}
# mean for factor vector
factor_mean = mean(pull(df, factor_vector))
factor_mean
```
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:
```{r results='hide'}
# 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.