简体   繁体   English

R:应用,自定义函数和数据框名称

[英]R: Apply, custom function and Dataframe names

I have a data frame Indices containing various names which correspond to data frames ( ie a name "Index 1" has a corresponding data frame Index 1 ). 我有一个数据帧Indices其中包含与数据帧相对应的各种名称( ,名称"Index 1"具有相应的数据帧Index 1 )。

Now I want to run my custom function calcScores over all data frames and add several columns to that data frame. 现在,我想在所有数据框架上运行自定义函数calcScores ,并向该数据框架添加几列。 Since I'm not in the global environment I return that "new" data frame and want to assign it back to the original variable Index 1 , so Index 1 now has the new data frame with the added columns. 由于我不在全局环境中,因此我返回了该“新”数据框,并希望将其分配回原始变量Index 1 ,因此Index 1现在具有带有添加列的新数据框。

Here's my code (no chance I can make this 100% reproducible since all data is very custom but I hope you understand my question). 这是我的代码(由于所有数据都是非常自定义的,所以我不可能将其100%重现,但希望您能理解我的问题)。

# Here the unique Index names are derived and stored
# Problem is the index names are stored as "Index 1", "Index 2" etc.
# Thats why I have to adjust These #titles and create individual data Frames
Indices <- unique(df[1])
apply(unique(df[1]), 1, function(x){
    assign(gsub(" ","",x,fixed=TRUE), subset(df,ticker==x), envir = .GlobalEnv)
})

calcRollingAverage <- function(Parameter, LookbackWindow){
    Output <- rollapply(Parameter, LookbackWindow, mean, fill=NA, partial=FALSE,
                        align="right")
}

calcScores<-function(Index, LookbackWindow){
    Index$PE_Avg = calcRollingAverage(Index$PE_Ratio, LookbackWindow)
    Index$PE_DIV_Avg = Index$PE_Ratio/Index$PE_Avg
    Index$PE_Score = cut(Index$PE_DIV_Avg, breaks=PE_Breaks, labels=Grades)

    return(Index)
}

apply(Indices,1,function(x) assign(gsub(" ","",x,fixed=TRUE), calcScores(get(gsub(" ","",x,fixed=TRUE)),lookback_window)))

I guess my problems are in the apply and with the whole get , assign and gsub story. 我想我的问题出在apply以及整个getassigngsub故事中。 The scoping is here obviously the issue... At the moment apply gives the following error: 范围界定显然是问题所在...目前,应用会产生以下错误:

Error: unexpected symbol in:
"apply(Indices,1,function(x) assign(gsub(" ","",x,fixed=TRUE), calcScores(get(gsub(" ","",x,fixed=TRUE)),lookback_window))
apply"

Ok solved it...sorry for the post. 好的,解决了这个问题。 That is the solution: envir = .GlobalEnv 那就是解决方案: envir = .GlobalEnv

apply(Indices,1,function(x) assign(gsub(" ","",x,fixed=TRUE), calcScores(get(gsub(" ","",x,fixed=TRUE)),lookback_window),envir = .GlobalEnv))

Why don't you just use a for loop, which would help with the scoping problems ? 您为什么不只使用for循环,这将有助于解决范围问题? Something like this : 像这样的东西:

mydf1 <- data.frame(x=1:3, y=2:4)
mydf2 <- data.frame(x=3:5, y=4:6)

indices <- c("mydf1","mydf2")

for (dfname in indices) {
    result <- get(dfname)
    result$z <- result$x+ result$y
    assign(dfname, result)
}

Looks like you want to split your data by ticker and apply a function on each splitted element. 看起来您想按代码分割数据并将一个函数应用于每个分割的元素。

This is a job for by or ddply in plyr package. 这是一个工作byddplyplyr包。

by(df,df$ticker,FUN=calcScores,LookbackWindow=lookback_window)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM