[英]R. How to replace numbers with dates or remove rows with numbers from data frame?
我有這樣的數據框:
[701] "38760" "38788" "38819" "38849" "38880" "38910" "38941"
[708] "38972" "39002" "39033" "39063" "12/13/2006" "12/14/2006" "12/15/2006"
[715] "12/16/2006" "12/17/2006" "12/18/2006" "12/19/2006" "12/20/2006" "12/21/2006" "12/22/2006"
[722] "12/23/2006" "12/24/2006" "12/25/2006" "12/26/2006" "12/27/2006" "12/28/2006" "12/29/2006"
[729] "12/30/2006" "12/31/2006"
我只想有日期(替換或刪除數字)。 任何人都知道一些合理的快速方法來做到這一點? 請幫忙。
假設我們在data.frame
中有一個“日期”列,它只包含兩種格式 - 1) %m/%d/%Y
和 2) 數值 - 然后我們應用as.Date
格式%m/%d/%Y
在整個“日期”上。 如果值是數字,它將返回NA
,我們單獨執行並在base R
中更新
dates1 <- with(df1, as.Date(dates, format = "%m/%d/%Y"))
dates1[is.na(dates1)] <- with(df1, as.Date(as.integer(dates[is.na(dates1)]),
origin = "1899-12-30"))
df1$dates <- dates1
-輸出
> df1$dates
[1] "2006-02-12" "2006-03-12" "2006-04-12" "2006-05-12" "2006-06-12" "2006-07-12" "2006-08-12" "2006-09-12" "2006-10-12" "2006-11-12" "2006-12-12" "2006-12-13"
[13] "2006-12-14" "2006-12-15" "2006-12-16" "2006-12-17" "2006-12-18" "2006-12-19" "2006-12-20" "2006-12-21" "2006-12-22" "2006-12-23" "2006-12-24" "2006-12-25"
[25] "2006-12-26" "2006-12-27" "2006-12-28" "2006-12-29" "2006-12-30" "2006-12-31"
df1 <- structure(list(dates = c("38760", "38788", "38819", "38849",
"38880", "38910", "38941", "38972", "39002", "39033", "39063",
"12/13/2006", "12/14/2006", "12/15/2006", "12/16/2006", "12/17/2006",
"12/18/2006", "12/19/2006", "12/20/2006", "12/21/2006", "12/22/2006",
"12/23/2006", "12/24/2006", "12/25/2006", "12/26/2006", "12/27/2006",
"12/28/2006", "12/29/2006", "12/30/2006", "12/31/2006")),
class = "data.frame", row.names = c(NA,
-30L))
我假設它是Excel
日期。 所以現在你可以使用as.Date
的origin
參數,即 1899 年 12 月 30 日。
library(dplyr)
df %>%
mutate(date = as.Date(date, origin = "1899-12-30"))
date Przedmiescie Centrum
1 2005-01-01 0 0
2 2005-02-01 0 0
3 2005-03-01 0 0
4 2005-04-01 0 0
5 2005-05-01 0 2
6 2005-06-01 0 0
數據:
structure(list(date = c(38353L, 38384L, 38412L, 38443L, 38473L,
38504L), Przedmiescie = c(0L, 0L, 0L, 0L, 0L, 0L), Centrum = c(0L,
0L, 0L, 0L, 2L, 0L)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
我有這樣的數據框:
date Przedmieście Centrum
<chr> <dbl> <dbl>
1 38353 0 0
2 38384 0 0
3 38412 0 0
4 38443 0 0
5 38473 0 2
6 38504 0 0
我有數字而不是日期,我需要有日期(從 2005 年到 2006 年)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.