-1
我有多個不同行長度的數據幀。我想將它們組合成一個數據框,但在我這樣做之前,我想要在數據框上循環多個功能。我的主要問題是我不知道如何執行多個數據框的多個功能。將多個參數組合到多個數據幀的循環中
我有文件名列表:對於例如dataframes名單是:
stationlist <- c("StationAlpha","StationBeta")
這兩個例子dataframes:
StationAlpha<-
structure(list(YEAR = c(1979L, 1979L, 1979L, 1979L, 1979L), MONTH = 8:12,
X1 = c(0, 5.6, 7.6, 0, 11), X2 = c(0, 2.6, 4.7, 0.4, 0),
X3 = c(0, 0, 0, 0, 0)), .Names = c("YEAR", "MONTH", "X1",
"X2", "X3"), row.names = c(NA, 5L), class = "data.frame") ### Example file 1
StationBeta<-
structure(list(YEAR = c(2001L, 2001L, 2001L, 2001L, 2001L), MONTH = 4:8,
X1 = c(2.5, 3.3, 18, 13.5, 0), X2 = c(0.9, 10, 0, 1.5, 0),
X3 = c(9, 0, 0, 7.5, 3.7)), .Names = c("YEAR", "MONTH", "X1",
"X2", "X3"), row.names = c(NA, 5L), class = "data.frame") ### Example file 2
的樣子:
YEAR|MONTH | X1 |X2 |X3 |etc.
...
2001| 4 |2.5 |0.9 |9.0 |...
2001| 5 |3.3 |10.0 |0.0 |...
2001| 6 |18.0 |0.0 |0.0 |...
2001| 7 |13.5 |1.5 |7.5 |...
2001| 8 |0.0 |0.0 |3.7 |...
....etc.
這些是我想要在所有數據幀上執行的功能:
test<-melt(StationAlpha, id=c("YEAR","MONTH"), variable.name = "day") #combine day columns to one big column
test$day<-gsub("X","",test$day);test$day<-as.numeric(test$day) # change days to numeric value
test$date <- as.Date(with(test, paste(YEAR, MONTH, day,sep="-")), "%Y-%m-%d") # make date from year,month,day
# make a data frame with dates and day of year
Ex.dates<-seq(as.Date("1940/01/01"), as.Date("2015/12/31"), "days") # vector with dates
df.append <-data.frame(Ex.dates); df.append$DayofYear<-df.append$Ex.dates; df.append$DayofYear<-strftime(df.append$DayofYear, format = "%j")
df.append$DayofYear<-as.numeric(df.append$DayofYear); names(df.append)<-c("date","DayOfYear")
# Combine both dataframes on column "date" and leaves out duplicates but keep empty rows # Method 2
df <- merge(df.append,test,by.x='date',by.y='date',all.x=T,all.y=T)
df$value<- ifelse(df$value < 0, -99, df$value) # replace negative values with -99
這是我想的dataframes合併成一個看起來像:
date |DayOfYear | YEAR |MONTH|day|StationAlpha|StationBeta|StationEtc
...
2001-05-01| 121 |2005 |5 |1 |5.6 |3.3 |5.1
2001-05-02| 122 |2005 |5 |2 |2.6 |10.0 |7.3
2001-05-03| 123 |2005 |5 |3 |0.0 |0.0 |0.0
2001-05-04| 124 |2005 |5 |4 |NA |NA |2.1
2001-05-05| 125 |2005 |5 |5 |NA |NA |0.0
....etc.
你能告訴我或暗示我,我怎麼會用一個有關這些例子dataframes循環?或者拉普利/薩普利,如果這給出了相同的結果?