2016-09-29 3 views
1

Ich bin neu in R, aber habe eine ziemlich gute Anleitung gefolgt, um auf dem Weg zu helfen.Erstellen einer Schleife zum Konvertieren von Rolling-Perioden auf as.Date

Ich habe Verkaufsdaten für die letzten 36 Monate importiert, tidyr verwendet, um die Date-Spalten in Zeilen zu verschieben, und dann die Datumzeichenfolge zu einem Month Date, Year gelöscht.

Meine Frage ist das: Ich muss diese Datei jeden Monat aktualisieren und wird eine neue, rollierende 36 Monate haben. Gibt es eine Verbesserung der Funktion, die ich erstellt habe, um die neuen Zeiträume zu automatisieren (z. B. Der aktuelle 36-monatige Zeitraum beginnt am 1. September 2013, der nächste 36-monatige beginnt am 1. Oktober 2013 usw.).

Haben Sie Ideen, wie Sie dies tun können, ohne ausschneiden und einfügen zu müssen?

Unten ist mein Code

# import rolling periods 
rolling <- read.csv("h:/R/BI with R/Rolling Periods.csv", header=T) 

# remove last four columns 
rolling <- rolling[,-c(42:45)] 

# gather columns to rows with tidyr 
require(tidyr) 
rolling <- gather(rolling, "Date", "CSE", 6:41) 

head(rolling) 

# list from Date column 
unique(rolling$Date) 

# clean Date variable 
clean = function(col) { 
    col = gsub('X1.Month.9.1.2013.thru.9.30.2013..Case.Equivs', 'September 1, 2013', col, fixed = TRUE) 
    col = gsub('X1.Month.10.1.2013.thru.10.31.2013..Case.Equivs', 'October 1, 2013', col, fixed = TRUE) 
    col = gsub('X1.Month.11.1.2013.thru.11.30.2013..Case.Equivs', 'November 1, 2013', col, fixed = TRUE) 
    col = gsub('X1.Month.12.1.2013.thru.12.31.2013..Case.Equivs', 'December 1, 2013', col, fixed = TRUE) 
    col = gsub('X1.Month.1.1.2014.thru.1.31.2014..Case.Equivs', 'January 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.2.1.2014.thru.2.28.2014..Case.Equivs', 'February 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.3.1.2014.thru.3.31.2014..Case.Equivs', 'March 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.4.1.2014.thru.4.30.2014..Case.Equivs', 'April 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.5.1.2014.thru.5.31.2014..Case.Equivs', 'May 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.6.1.2014.thru.6.30.2014..Case.Equivs', 'June 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.7.1.2014.thru.7.31.2014..Case.Equivs', 'July 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.8.1.2014.thru.8.31.2014..Case.Equivs', 'August 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.9.1.2014.thru.9.30.2014..Case.Equivs', 'September 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.10.1.2014.thru.10.31.2014..Case.Equivs', 'October 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.11.1.2014.thru.11.30.2014..Case.Equivs', 'November 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.12.1.2014.thru.12.31.2014..Case.Equivs', 'December 1, 2014', col, fixed = TRUE) 
    col = gsub('X1.Month.1.1.2015.thru.1.31.2015..Case.Equivs', 'January 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.2.1.2015.thru.2.28.2015..Case.Equivs', 'February 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.3.1.2015.thru.3.31.2015..Case.Equivs', 'March 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.4.1.2015.thru.4.30.2015..Case.Equivs', 'April 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.5.1.2015.thru.5.31.2015..Case.Equivs', 'May 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.6.1.2015.thru.6.30.2015..Case.Equivs', 'June 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.7.1.2015.thru.7.31.2015..Case.Equivs', 'July 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.8.1.2015.thru.8.31.2015..Case.Equivs', 'August 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.9.1.2015.thru.9.30.2015..Case.Equivs', 'September 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.10.1.2015.thru.10.31.2015..Case.Equivs', 'October 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.11.1.2015.thru.11.30.2015..Case.Equivs', 'November 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.12.1.2015.thru.12.31.2015..Case.Equivs', 'December 1, 2015', col, fixed = TRUE) 
    col = gsub('X1.Month.1.1.2016.thru.1.31.2016..Case.Equivs', 'January 1, 2016', col, fixed = TRUE) 
    col = gsub('X1.Month.2.1.2016.thru.2.28.2016..Case.Equivs', 'February 1, 2016', col, fixed = TRUE) 
    col = gsub('X1.Month.3.1.2016.thru.3.31.2016..Case.Equivs', 'March 1, 2016', col, fixed = TRUE) 
    col = gsub('X1.Month.4.1.2016.thru.4.30.2016..Case.Equivs', 'April 1, 2016', col, fixed = TRUE) 
    col = gsub('X1.Month.5.1.2016.thru.5.31.2016..Case.Equivs', 'May 1, 2016', col, fixed = TRUE) 
    col = gsub('X1.Month.6.1.2016.thru.6.30.2016..Case.Equivs', 'June 1, 2016', col, fixed = TRUE) 
    col = gsub('X1.Month.7.1.2016.thru.7.31.2016..Case.Equivs', 'July 1, 2016', col, fixed = TRUE) 
    col = gsub('X1.Month.8.1.2016.thru.8.31.2016..Case.Equivs', 'August 1, 2016', col, fixed = TRUE) 
    return(col) 
} 

rolling$Date = clean(rolling$Date) 
head(rolling) 

# convert to Date object 
rolling$Date = as.Date(rolling$Date, format="%B %d, %Y") 
head(rolling) 
+0

das Datum Parsen: 'roll $ Date_clean <- as.Date (Roll $ Datum, 'X1.Monat.% M.% D.% Y') '. Sie können dann 'format (rolling $ Date_clean, '% B% e,% Y')' verwenden, wenn Sie _really_ ein Zeichendatum benötigen. – alistaire

Antwort

1

Es Sie scheint an dem ersten Teil des Rolltermin suchen den Vertreter Datum (das Datum, das Sie ersetzen) zu bestimmen. In diesem Fall können Sie die Vorteile von Rückreferenzierungen Ersatz nehmen könnte, wenn gsub und regulären Ausdruck

# Function might not be need now 
clean <- function(col) { 
    col <- gsub("X1\\.Month\\.(\\d{1,2})\\.(\\d{1,2})\\.(\\d{4}).thru.*","\\1 \\2 \\3", col) 
} 

head(rolling) 

# convert to Date object 
#Note the new date fomat 
rolling$Date <- as.Date(rolling$Date, format="%m %d %Y") 
head(rolling) 

Wenn Sie auch das Enddatum des Rolldatum benötigen, können Sie noch mehr Rückreferenzierungen verwenden könnte das Enddatum zu erfassen.

1

Konvertieren Sie die Spalte zu einem Datenrahmen Punkt als Feldtrennzeichen und anschließend Format sprintf und month.name wie folgt aus:

clean2 <- function(x) { 
    DF <- read.table(text = x, sep = ".") 
    with(DF, sprintf("%s %d, %d", month.name[V3], V4, V5)) 
} 

die letzte Anweisung abwechselnd geschrieben haben könnte als:

with(DF, format(as.Date(paste(V5, V3, V4, sep = "-")), "%B %e, %Y")) 

Beispiel

Zum Beispiel mit dem Eingang in der Anmerkung unten:

clean2(x) 
## [1] "September 1, 2013" "October 1, 2013" "November 1, 2013" 

Hinweis: Hier einige Beispieldaten in reproduzierbarer Form ist:

x <- c("X1.Month.9.1.2013.thru.9.30.2013..Case.Equivs", 
     "X1.Month.10.1.2013.thru.10.31.2013..Case.Equivs", 
     "X1.Month.11.1.2013.thru.11.30.2013..Case.Equivs") 
Verwandte Themen