如何在R中将多个data.frames作为输入应用自定义函数?

时间:2022-01-01 12:48:28

I'm trying to use a custom function that takes a row of data from one data.frame (raw_DF) and uses calibration data from a separate data.frame (calibrant_DF) and then calculates the calibrated value of Raw12. I am getting this error:

我正在尝试使用自定义函数从一个data.frame(raw_DF)获取一行数据,并使用来自单独data.frame(calibrant_DF)的校准数据,然后计算Raw12的校准值。我收到此错误:

Error in cal_DF$Cal_set : $ operator is invalid for atomic vectors Called from: top level

cal_DF $ Cal_set中的错误:$运算符对于原子向量无效:来自:top level

It seems that the apply function doesn't like having a data.frame passed to it this way so I've spent the past few hours trying to figure out if I can use a different apply function (e.g. mapply, lapply) or a plyr function to accomplish what I'm trying to do but without any luck. Suggestions?

似乎apply函数不喜欢以这种方式传递给它的data.frame所以我花了几个小时试图弄清楚我是否可以使用不同的apply函数(例如mapply,lapply)或plyr功能,以完成我正在尝试做但没有任何运气。建议?

Example Code (real functions and DF's are more complicated):

示例代码(实际函数和DF更复杂):

raw_DF<-data.frame("Cal_set"=c(1,2,1,2),"Raw12"=c(3.3,3.1,5.1,4.2))
calibrant_DF<-data.frame("Cal_set"=c(1,2),"b12"=c(.01,.04),"m12"=c(.95,.99))

apply.cals <- function(raw_row,cal_DF){
  current_cals<-cal_DF[which(cal_DF$Cal_set==raw_row$Cal_set),]
  raw12<-raw_row$Raw12
  cal12<-(raw12-current_cals$b12)/current_cals$m12

  outdata<-data.frame(raw12,cal12)
  return(outdata)
} # End of apply.cals

calibrated_data<-apply(X=raw_DF,MARGIN=1,FUN=apply.cals,cal_DF="calibrant_DF")

And my desired output is a data.frame (or something I can put into a data.frame) of results like this:

我想要的输出是data.frame(或者我可以放入data.frame中)的结果,如下所示:

raw12 cal12 3.3 3.463158 3.1 3.090909 5.1 5.357895 4.2 4.20202

raw12 cal12 3.3 3.463158 3.1 3.090909 5.1 5.357895 4.2 4.20202

Thanks for any advice!

谢谢你的建议!

EDIT - SOLVED, BUT.... I'd be interested in plyr solutions if anyone else has one in mind - that's a function I'd like to understand better and my impression is that this a problem it could deal with elegantly.

编辑 - 解决,但....如果其他人有一个想法,我会对plyr解决方案感兴趣 - 这是一个我想更好理解的功能,我的印象是这个问题可以优雅地处理。

1 个解决方案

#1


1  

apply expects a matrix - and if it gets a data frame, it will convert it to a matrix. So you can't rely on $ with apply.

apply需要一个矩阵 - 如果它得到一个数据框,它会将它转换为矩阵。所以你不能依赖$申请。

One way to quickly convert your code to something that works is:

将代码快速转换为有效代码的一种方法是:

sapply(split(raw_DF, rownames(raw_DF)), apply.cals, cal_DF=calibrant_DF)

split(raw_df, rownames(raw_DF)) converts raw_DF into a list, where each component is a data frame with just one row. And sapply applies your function to each such data frame.

split(raw_df,rownames(raw_DF))将raw_DF转换为列表,其中每个组件只是一行的数据框。并且将您的功能应用于每个此类数据框。

What i get in this example is:

我在这个例子中得到的是:

#       1        2        3        4      
# raw12 3.3      3.1      5.1      4.2    
# cal12 3.463158 3.090909 5.357895 4.20202

(I hope the output makes any sense to you ... )

(我希望输出对你有意义......)

#1


1  

apply expects a matrix - and if it gets a data frame, it will convert it to a matrix. So you can't rely on $ with apply.

apply需要一个矩阵 - 如果它得到一个数据框,它会将它转换为矩阵。所以你不能依赖$申请。

One way to quickly convert your code to something that works is:

将代码快速转换为有效代码的一种方法是:

sapply(split(raw_DF, rownames(raw_DF)), apply.cals, cal_DF=calibrant_DF)

split(raw_df, rownames(raw_DF)) converts raw_DF into a list, where each component is a data frame with just one row. And sapply applies your function to each such data frame.

split(raw_df,rownames(raw_DF))将raw_DF转换为列表,其中每个组件只是一行的数据框。并且将您的功能应用于每个此类数据框。

What i get in this example is:

我在这个例子中得到的是:

#       1        2        3        4      
# raw12 3.3      3.1      5.1      4.2    
# cal12 3.463158 3.090909 5.357895 4.20202

(I hope the output makes any sense to you ... )

(我希望输出对你有意义......)