所以這是 R 中的一個基本演算法,它列印出兩個日期之間的日期。
initial_date <- as.Date(toString((readline(prompt = "Enter a starting date in the format year-month-day:"))))
final_date <- as.Date(toString((readline(prompt = "Enter a final in the format year-month-day:"))))
dates <- seq(final_date, initial_date, by = "-1 day")
rev(dates[dates > initial_date & dates < final_date])
max.print = length(dates)
print(dates)
我想修改它,以便日期的格式為月-日-年:2008 年 11 月 27 日。所以我添加了“format(dates, format="%b %d %Y")”。
initial_date <- as.Date(toString((readline(prompt = "Enter a starting date in the format year-month-day:"))))
final_date <- as.Date(toString((readline(prompt = "Enter a final in the format year-month-day:"))))
dates <- seq(final_date, initial_date, by = "-1 day")
format(dates, format="%b %d %Y")
rev(dates[dates > initial_date & dates < final_date])
max.print = length(dates)
print(dates)
但這會繼續列印與之前代碼相同的輸出。我如何解決它?
uj5u.com熱心網友回復:
這里有幾點誤解:
format(dates, format="%b %d %Y")可能會按照您希望的方式對其進行格式化,但它并未被存盤,因此使用的下一個命令dates是使用物件,就像呼叫format(..). 這以及大多數R 函式都是函式式的,這意味著它們的效果在存盤在物件中時實作:呼叫函式本身沒有副作用。“正確”的使用方法format是立即列印(見下文)或將其存盤到相同或另一個變數中。雖然我不建議這樣做,但更實用的用途是dates <- format(dates, format="%b %d %Y")同上
rev(dates[...]):您需要立即使用它(如print(rev(...)),即立即函式呼叫的引數)或將其存盤在其他地方,例如reversed_dates <- rev(dates[...])在 R 中,日期(proper
Date-class)是類似數字的,因此可以安全地進行連續數字比較,例如date1 < date2anddate2 >= date3等。但是,如果您不小心將%Y-%m-%d-字串與另一個類似格式的字串進行比較,那么它仍然會作業。它仍然有效,因為字串是按字典順序比較的。這意味著當比較字串"2020-01-01"and 時"2019-01-01",它會先比較"2"and"2",它是平局;與"0"s相同;然后它會看到"2">"1",因此"2019-01-01"在另一個之前。這仍然有效,即使作為字串,因為最重要的組件是年份,并且只要它們在字串中排在第一位,相對順序 (
>,sort,order) 仍然有效。如果日期是0填充的整數,這將繼續作業。這并沒有作業,如果他們不0-padded,其中"2021-2-1" > "2021-11-1"報告為TRUE; 這是因為它得到月份部分和比較"2"與第一"1"的"11",并沒有看到,在未來的數字使得"1"大于"2"。當人們開始引入月份名稱時,就會出現同樣型別的錯誤,因為月份名稱(可能是任何語言?)不是按字典順序排列的(我不知道這是絕對真理,但肯定是在英語和也許許多/大多數西方語言中都是如此......我不會說其他語言的多語種)。不幸的
"2020-Apr-01" < "2020-Jan-01"是TRUE,這意味著將再次成為。我們將#3 與一般情況下,R 總是將
Date-class 物件列印為這樣的事實相結合"%Y-%m-%d";沒有(微不足道的)方法可以讓它Date像您一樣列印-class 物件,"%b %d %Y"而沒有(a)將其轉換為字串并失去正確的順序;或 (b) 對其進行超級分類,使其在控制臺上按照您想要的方式呈現,但它仍然是下面的數字。至于(a),這是對圖中的報告和標簽所做的常見事情,我對此非常滿意。我并不是要說服世界它應該始終將日期視為
%Y-%m-%d. 但是,我要說的是,Date在您實際渲染它之前將其保留為適當的類物件要容易得多,然后format在最后一秒才將其保留。為此,請先執行所有過濾和排序,然后再執行print(format(..)),例如此。我推薦這種方法。dates <- seq(as.Date("2020-02-02"), as.Date("2020-02-06"), by = "day") dates <- rev(dates[ dates > as.Date("2020-02-03") ]) print(format(dates, format = "%b %d %Y")) # [1] "Feb 06 2020" "Feb 05 2020" "Feb 04 2020"同樣,以上是我推薦的技術。
至于 (b),是的,你可以做到,但這種方法是脆弱的,因為一些需要
Date-class 物件的函式不會立即意識到它們足夠接近以繼續作業是可行的;或者他們將剝離我們分配的新類,此時它將訴諸"%Y-%m-%d"-format。您可以使用它,這要求您更改要個性化格式# important的每個Date-object的類(請參閱該行)。我建議不要這樣做。format.myDATE <- function(x, ...) { # fashioned after format.Date xx <- format.Date(x, format = "%b %d %Y") names(xx) <- names(x) xx } print.myDATE <- function(x, max = NULL, ...) { # fashioned after print.Date if (is.null(max)) max <- getOption("max.print", 9999L) if (max < length(x)) { print(format.myDATE(x[seq_len(max)]), ...) cat(" [ reached 'max' / getOption(\"max.print\") -- omitted", length(x) - max, "entries ]\n") } else if (length(x)) print(format.myDATE(x), ...) else cat(class(x)[1L], "of length 0\n") invisible(x) } dates <- seq(as.Date("2020-02-02"), as.Date("2020-02-06"), by = "day") class(dates) <- c("myDATE", class(dates)) ## important! dates <- rev(dates[ dates > as.Date("2020-02-03") ]) print(dates) ## no need for format! # [1] "Feb 06 2020" "Feb 05 2020" "Feb 04 2020" ### and number-like operations still tend to work diff(dates) # Time differences in days # [1] -1 -1Again, I recommend against doing this for data that you are working with. Many packages that pretty-print tables and plots and such may choose to override our preference for formatting, so there is no guarantee that this is honored across the board. This is why I suggest "accepting" the R way while working with it, regardless of your locale, and formatting it for your aesthetic preferences immediately before printing/rendering.
Another couple minor points:
- remove
toString, it's doing nothing for you here I think; - your use of
max.print = ...suggests you think this is going to change anything else; most R things that have global options useoptions(...)for this, so you need to either set it globally in this R session withoptions(max.print=length(dates)), or a one-time limit withprint(dates, max = length(dates)).
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/350569.html
上一篇:Excel日期公式 3個月
下一篇:正確讀取日期/時間值
