我无法使用给定变量创建绘图
I cannot create the plot with given variables
我是基本 R user.However 我无法创建显示 weather
数据集的每日最低温度(以 F 为单位)的图表,该数据集在 nycflights13
中给出。我需要用 str_c
创建一个名为 date
的新列,这样 date
应该按如下方式给出“YYYY-MM-DD”。我使用 lubridate 包但它给出了错误。
有人对此有解释吗?
lubridate::make_date
可以解决问题
下面是文档。
make_date(year = 1970L, month = 1L, day = 1L)
library(tidyverse)
library(nycflights13)
library(lubridate)
df <- weather
df <- df %>%
mutate(date = make_date(year, month, day)) %>%
group_by(date) %>%
summarize(min_temp = min(temp)*1.8 + 32)
> df
# A tibble: 364 x 2
date min_temp
<date> <dbl>
1 2013-01-01 80.5
2 2013-01-02 73.4
3 2013-01-03 78.9
4 2013-01-04 84.1
5 2013-01-05 89.6
6 2013-01-06 91.5
7 2013-01-07 89.6
8 2013-01-08 84.1
9 2013-01-09 93.2
10 2013-01-10 102.
# ... with 354 more rows
ggplot(df) + geom_point(aes(x = date, y = min_temp))
此外,这可能会解决您的代码问题。
as.Date(str_c(weather$year, "-", weather$month, "-", weather$day), format = "%Y-%m-%d")
我是基本 R user.However 我无法创建显示 weather
数据集的每日最低温度(以 F 为单位)的图表,该数据集在 nycflights13
中给出。我需要用 str_c
创建一个名为 date
的新列,这样 date
应该按如下方式给出“YYYY-MM-DD”。我使用 lubridate 包但它给出了错误。
有人对此有解释吗?
lubridate::make_date
可以解决问题
下面是文档。
make_date(year = 1970L, month = 1L, day = 1L)
library(tidyverse)
library(nycflights13)
library(lubridate)
df <- weather
df <- df %>%
mutate(date = make_date(year, month, day)) %>%
group_by(date) %>%
summarize(min_temp = min(temp)*1.8 + 32)
> df
# A tibble: 364 x 2
date min_temp
<date> <dbl>
1 2013-01-01 80.5
2 2013-01-02 73.4
3 2013-01-03 78.9
4 2013-01-04 84.1
5 2013-01-05 89.6
6 2013-01-06 91.5
7 2013-01-07 89.6
8 2013-01-08 84.1
9 2013-01-09 93.2
10 2013-01-10 102.
# ... with 354 more rows
ggplot(df) + geom_point(aes(x = date, y = min_temp))
此外,这可能会解决您的代码问题。
as.Date(str_c(weather$year, "-", weather$month, "-", weather$day), format = "%Y-%m-%d")