我是堆栈溢出和 R 的新手
我想监测睡眠时间,所以我发现这个项目非常好。 我以为我会将数据写入.csv。有些像这样:
Date ,hms1,hms2
2005-01-01,00:00:00,09:17:00
2005-01-02,00:12:00,10:02:00
2005-01-03,00:15:00,08:02:00
2005-01-04,01:32:00,07:04:00
2005-01-05,02:34:00,05:04:00
2005-01-06,03:34:00,05:05:00
2005-01-07,01:02:00,04:04:00
2005-01-08,00:12:00,04:04:00
2005-01-09,04:32:00,09:04:00
2005-01-10,06:33:00,10:02:00
2005-01-11,01:02:00,08:05:00
2005-01-12,22:30:00,03:03:00
2005-01-13,02:00:00,05:55:00
我不知道如何从 R 中的.csv读取数据。我尝试过使用以下代码:
Data <- read.csv("hora2.csv", sep = ";", Headers = TRUE, stringAsFactors = FALSE)
但这没有用。
我正在工作的代码是这样的:
library(ggplot2)
library(scales)
library(stringr)
#hms Hora Minutos Segundos
df1$Date = as.POSIXct(df1$Date) + 3600*8
set.seed(20)
df1$hms1a = df1$Date + runif(nrow(df1), 3600*5, 3600*10)
df1$hms2a = df1$Date + runif(nrow(df1), 3600*15, 3600*66)
ggplot(df1, aes(x=Date)) +
scale_x_datetime(breaks = date_breaks("1 day")) +
scale_y_continuous(limits = c(0,48), breaks=seq(0,48,2),
labels=str_pad(seq(0,48,2) %% 24, 2, pad="0")) +
geom_hline(yintercept=seq(0,48,24)) +
geom_linerange(aes(ymin = hms1a - Date, ymax = hms2a - Date), color = "#63C1FF",size = 5) +
coord_flip() + ylab("Tiempo en horas") +
ggtitle("Horas de Sueño")
由于您的数据由,
分隔,因此应使用,
作为分隔符。因此请尝试
Data <- read.csv("hora2.csv", sep = ",", header = TRUE, stringsAsFactors = FALSE)
而不是
Data <- read.csv("hora2.csv", sep = ";", Headers = TRUE, stringAsFactors = FALSE)