示例原始資料:
1
Mon Apr 06 23:55:14 PDT 2009
2
Tue Apr 07 01:16:43 PDT 2009
3
Tue Apr 07 03:06:17 PDT 2009
由于我有來自 csv 檔案的資料,因此我在使用 strptime 進行格式設定時遇到了問題。
dates <- read.csv(file = "Australia_timestamp.csv")
colnames(dates) <- c("Date")
format <- "%a %b %d %H:%M:%S %z %Y"
dates <-strptime(dates[], format = format)
輸出:
NA
如何通過沒有NA正確格式的輸出來解決這個問題?
uj5u.com熱心網友回復:
我們可能會使用parse_date從parsedate
library(tibble)
library(parsedate)
lines1 <- lines[nzchar(lines)]
tibble(num = lines1[c(TRUE, FALSE)], time = parse_date(lines1[c(FALSE, TRUE)]))
-輸出
# A tibble: 3 × 2
num time
<chr> <dttm>
1 1 2009-04-06 23:55:14
2 2 2009-04-07 01:16:43
3 3 2009-04-07 03:06:17
資料
lines <- readLines(textConnection("1
Mon Apr 06 23:55:14 PDT 2009
2
Tue Apr 07 01:16:43 PDT 2009
3
Tue Apr 07 03:06:17 PDT 2009"))
# or read from the file
lines <- readLines("file.txt")
uj5u.com熱心網友回復:
您得到的 NA 是因為 %z 不捕獲 PDT。它將屬于,%Z因為它不是數字,但strptime不支持%Z輸入,因為字符縮寫不是明確的。PDT 是 UTC-7,因此要使用 捕獲時間戳strptime,您的字串必須是,例如,
Tue Apr 07 03:06:17 -0700 2009
您可以采取哪些措施來解決問題取決于您擁有的資料。如果您所有的時間戳都在 PDT 中,您可以簡單地將所有字串中的 PDT 替換為 -0700:
dates$Date <- gsub("PDT", "-0700", dates$Date)
dates$Date <- strptime(dates$Date, format = format)
uj5u.com熱心網友回復:
1)主題指的是 csv 檔案,但顯示的資料不是 csv 格式。我們將假設問題中顯示的表單是正確的,并且輸入檔案包含第一行只有 1,第二行描述日期/時間,第三行是空白,依此類推,如最后的注釋。
我們一行一行地讀取它,然后用num: ,datetime: 和一個空字串作為后續行的前綴。這會將其轉換為 debian 控制格式形式 (dcf),因此我們可以使用read.dcf它來讀取它并給出一個字符矩陣。將其轉換為資料框,使用 convert.type 將第一列轉換為數字,并將第二列轉換為 POSIXct。
下面管道中的前兩行是Lines為了可重復性而在注釋中使用,但要從檔案中讀取相同的內容,只需將管道中的前兩行替換為類似的內容,"myfile" |>其中myfile是輸入檔案的名稱。
不使用任何包。
prefix <- \(x) paste(rep(c("num:", "time:", ""), length = length(x)), x)
Lines |>
textConnection(name = "") |>
readLines() |>
trimws() |>
prefix() |>
textConnection(name = "") |>
read.dcf() |>
as.data.frame() |>
type.convert(as.is = TRUE) |>
transform(time = as.POSIXct(time, format = "%a %b %d %H:%M:%S PDT %Y"))
給出這個 data.frame:
num time
1 1 2009-04-06 23:55:14
2 2 2009-04-07 01:16:43
3 3 2009-04-07 03:06:17
2)另一種方法是將資料轉換為矩陣,如圖所示,然后按上述步驟進行。
Lines |>
textConnection(name = "") |>
read.table(sep = "?", strip.white = TRUE) |>
unlist() |>
matrix(ncol = 2, byrow = TRUE, dimnames = list(NULL, c("num", "time"))) |>
as.data.frame() |>
type.convert(as.is = TRUE) |>
transform(time = as.POSIXct(time, format = "%a %b %d %H:%M:%S PDT %Y"))
3)如果問題中顯示的格式是錯誤的,這意味著 1, 2, 3, ... 出現在同一行的日期時間之前,然后逐行讀取,將第一個空格替換為一個逗號并將其讀入具有指定列名稱的資料框中。最后將時間列轉換為 POSIXct。
Lines2 <- "1 Mon Apr 06 23:55:14 PDT 2009
2 Tue Apr 07 01:16:43 PDT 2009
3 Tue Apr 07 03:06:17 PDT 2009"
Lines2 |>
textConnection(name = "") |>
readLines() |>
trimws() |>
sub(pattern = " ", replacement = ",") |>
textConnection(name = "") |>
read.table(sep = ",", col.names = c("num", "time"), strip.white = TRUE) |>
transform(time = as.POSIXct(time, format = "%a %b %d %H:%M:%S PDT %Y"))
4) If the input is truly a csv file but with no column headers as shown in Lines3 then it is even easier:
Lines3 <- "1,Mon Apr 06 23:55:14 PDT 2009
2,Tue Apr 07 01:16:43 PDT 2009
3,Tue Apr 07 03:06:17 PDT 2009"
Lines3 |>
textConnection(name = "") |>
read.table(sep = ",", col.names = c("num", "time"), strip.white = TRUE) |>
transform(time = as.POSIXct(time, format = "%a %b %d %H:%M:%S PDT %Y"))
Note
Lines <- "1
Mon Apr 06 23:55:14 PDT 2009
2
Tue Apr 07 01:16:43 PDT 2009
3
Tue Apr 07 03:06:17 PDT 2009"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314425.html
上一篇:Rvest無法識別表格
