For loop 将多个参数组合到多个数据帧的循环中

For loop 将多个参数组合到多个数据帧的循环中,for-loop,dataframe,lapply,For Loop,Dataframe,Lapply,我有多个不同行长度的数据帧。我希望将它们合并到一个数据帧中,但在此之前,我希望在数据帧上循环多个函数。我的主要问题是我不知道如何对多个数据帧执行多个函数 我有一个文件名列表:对于示例数据帧,该列表为: stationlist <- c("StationAlpha","StationBeta") 以下是我希望在所有数据帧上执行的函数: StationAlpha<- structure(list(YEAR = c(1979L, 1979L, 1979L, 1979L, 1979L),

我有多个不同行长度的数据帧。我希望将它们合并到一个数据帧中,但在此之前,我希望在数据帧上循环多个函数。我的主要问题是我不知道如何对多个数据帧执行多个函数

我有一个文件名列表:对于示例数据帧,该列表为:

stationlist <- c("StationAlpha","StationBeta")
以下是我希望在所有数据帧上执行的函数:

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
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

您能告诉我或提示我如何在这些示例数据帧上使用for循环吗?或lappy/sapply,如果结果相同?

如何将函数应用于每个数据帧:

1) 列出数据帧

stationlist<-mget(ls(pattern="X9")) 
# just made a list of data frames starting with "X9" as this is characteristic for my type dataframe names, but for the example the following is sufficient:
stationlist<-list(StationAlpha,StationBeta)
stationlist
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.
stationlist<-mget(ls(pattern="X9")) 
# just made a list of data frames starting with "X9" as this is characteristic for my type dataframe names, but for the example the following is sufficient:
stationlist<-list(StationAlpha,StationBeta)
my.melt <- function(x){
  x <- melt(x, id.vars=c("YEAR","MONTH"), 
            variable.name="day")
  x
} # melt function in order to be able to melt the days of the month

for (i in 1:length(stationlist)){
  stationlist[[i]] <- my.melt(stationlist[[i]]) 
} # melt the days of the month

for (i in 1:length(kenyastationlist)){
  stationlist[[i]]$day <- gsub("X","",stationlist[[i]]$day) 
} #remove the X from the stringvalue, before turning it into a number of day 
Combined_df<- Reduce(function(x, y) merge(x, y, all=TRUE),stationlist) # combine all into one dataframe