我试图将我的代码编织到一个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")发布于 2020-02-05 09:15:25
我不确定,您试图用line执行的是什么:
dt[,i] <- apply(dt[,i], 2, function(x) as.numeric(as.character(x)))
但我猜,您正在尝试将Jan和Feb列转换为numeric,它将被第一个命令读取为char,如果是这样,则使用:
dt[,2:3] <- apply(dt[,2:3], 2, function(x) as.numeric(as.character(x)))
仅显式转换这两列。尝试在更改后编织代码,它应该会成功生成html。
我建议使用str(dt)来检查哪些列被读取为哪种类型。
https://stackoverflow.com/questions/60067503
复制相似问题