简体   繁体   English

具有两个参数功能的for循环的更快替代方法

[英]faster alternative to for loop with two-argument function

I have a list of csv files called sourcefiles, and I want to apply a function with two arguments to all of files in sourcefiles. 我有一个称为sourcefiles的csv文件列表,我想对带有源文件的所有文件应用两个参数的函数。 Here's what I'm doing now: 这是我现在正在做的事情:

 for (n in 1:length(sourcefiles)){
      clcc(DT, n)
    }

Is there any better way? 有什么更好的办法吗?

Thanks! 谢谢!

You can use lapply function: 您可以使用lapply函数:

lapply(X=aList, FUN=aFunction, otherParameters)

This function call aFunction for every item of the aList passing it as a first parameter and otherParameters as the other parameters. 此函数为aList每个项目调用aFunction ,将其作为第一个参数传递,将otherParameters作为其他参数传递。

The problem here is that your function clcc does not take the sourcefile as first parameter, but there is an easy workaround. 这里的问题是您的函数clcc不会将源文件作为第一个参数,但是有一个简单的解决方法。 If the formal name of the first parameter of the function clcc is DT (or whatever), you can call lapply by setting the name of it: 如果函数clcc的第一个参数的正式名称是DT (或其他名称),则可以通过设置其名称来调用lapply:

lapply(X=sourcefiles, FUN=clcc, DT=DT)

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

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