我们有一个非常大的数据帧df,可以按因子分割.在由此拆分创建的数据帧的每个子集上,我们需要执行操作以增加该子集的行数,直到它达到一定长度.之后,我们对子集进行调整以获得更大版本的df.
假设我们的子集操作(在单独的.R文件中)是:
foo< - function(df){magic} 我们想出了几种方法: 1)
df <- split(df,factor) df <- lapply(df,foo) rbindlist(df)
2)
assign('list.df',list(),envir=.GlobalEnv) assign('i',1,envir=.GlobalEnv) dplyr::group_by(df,factor) dplyr::mutate(df,foo.list(df.col)) df <- rbindlist(list.df) rm('list.df',envir=.GlobalEnv) rm('i',envir=.GlobalEnv) (In a separate file) foo.list <- function(df.cols) { magic; list.df[[i]] <<- magic.df i <<- i + 1 return(dummy) }
第一种方法的问题是时间问题. lapply只需要太长时间才能真正理想(使用我们的数据集大约一个小时).
第二种方法的问题是篡改用户的全球环境的非常不希望的副作用.它明显更快,但如果可以的话,这是我们宁愿避免的.
我们也尝试过传入列表并计算变量,然后尝试用父环境中的变量替换它们(一种黑客来解决R缺乏传递引用).
我们已经研究了一些可能相关的SO问题(R applying a function to a subset of a data frame,Calculations on subsets of a data frame,R: Pass by reference,e.t.c.),但没有一个问题太过清楚.
x <- runif(n=10,min=0,max=3) y <- sample(x=10,replace=FALSE) factors <- runif(n=10,max=2) factors <- floor(factors) df <- data.frame(factors,x,y)
## We group by factor,then run foo on the groups. foo <- function(df.subset) { min <- min(df.subset$y) max <- max(df.subset$y) ## We fill out df.subset to have everything between the min and ## max values of y. Then we assign the old values of df.subset ## to the corresponding spots. df.fill <- data.frame(x=rep(0,max-min+1),y=min:max,factors=rep(df.subset$factors[1],max-min+1)) df.fill$x[which(df.subset$y %in%(min:max))] <- df.subset$x df.fill }