R markdown 编织错误没有选择 tidyselect 变量
R markdown knitting error no tidyselect variables selected
我正在尝试将我的代码编织到一个 html 文件中;但是,当文件到达包含函数 pivot_longer()
的代码行时,会出现错误 (see error here)。我将在下面附上我的代码。有什么建议么?
dataset (.csv file)
library(ggplot2)
library(dslabs)
library(dplyr)
library(tidyr)
library(tidyverse)
dt <- read.csv("~co2-2019.csv", stringsAsFactors = FALSE)
dt[,i] <- apply(dt[,i], 2, function(x) as.numeric(as.character(x)))
dt.long <- pivot_longer(dt, -year, names_to = "month", values_to = "co2")
dt.long <- mutate(dt.long, month = factor(month, levels = month.abb))
dt.long <- group_by(dt.long, year, month) %>% summarize(mean_co2 = mean(co2, na.rm = TRUE))
ggplot(dt.long, aes(x=month, y=mean_co2, group = year))+ggtitle("Average Annual CO2 Levels at Mauna Loa Observatory")+geom_line()+labs(x="Month", y = "Average CO2 Level")
我不确定,你想用以下行执行什么:
dt[,i] <- apply(dt[,i], 2, function(x) as.numeric(as.character(x)))
但我想,您正在尝试将 Jan 和 Feb 列转换为数字,这将被第一个命令读取为 char,如果是这样,则使用:
dt[,2:3] <- apply(dt[,2:3], 2, function(x) as.numeric(as.character(x)))
仅显式转换这两列。尝试在更改后编写代码,它应该会成功生成 html。
我建议使用 str(dt)
检查哪些列被读取为何种类型。
我正在尝试将我的代码编织到一个 html 文件中;但是,当文件到达包含函数 pivot_longer()
的代码行时,会出现错误 (see error here)。我将在下面附上我的代码。有什么建议么?
dataset (.csv file)
library(ggplot2)
library(dslabs)
library(dplyr)
library(tidyr)
library(tidyverse)
dt <- read.csv("~co2-2019.csv", stringsAsFactors = FALSE)
dt[,i] <- apply(dt[,i], 2, function(x) as.numeric(as.character(x)))
dt.long <- pivot_longer(dt, -year, names_to = "month", values_to = "co2")
dt.long <- mutate(dt.long, month = factor(month, levels = month.abb))
dt.long <- group_by(dt.long, year, month) %>% summarize(mean_co2 = mean(co2, na.rm = TRUE))
ggplot(dt.long, aes(x=month, y=mean_co2, group = year))+ggtitle("Average Annual CO2 Levels at Mauna Loa Observatory")+geom_line()+labs(x="Month", y = "Average CO2 Level")
我不确定,你想用以下行执行什么:
dt[,i] <- apply(dt[,i], 2, function(x) as.numeric(as.character(x)))
但我想,您正在尝试将 Jan 和 Feb 列转换为数字,这将被第一个命令读取为 char,如果是这样,则使用:
dt[,2:3] <- apply(dt[,2:3], 2, function(x) as.numeric(as.character(x)))
仅显式转换这两列。尝试在更改后编写代码,它应该会成功生成 html。
我建议使用 str(dt)
检查哪些列被读取为何种类型。