简体   繁体   English

如何在R中扩展函数以在不同数据集上并行运行?

[英]How can I extend a function in R to run parallely on different datasets?

I have made a function find.string() which accepts a string and outputs different patterns in it. 我做了一个函数find.string() ,它接受一个字符串并在其中输出不同的模式。

For example: find.string("abcabcabc") - "abc" 例如:find.string(“ abcabcabc”)-“ abc”

Now, what I wish to do is, I have two large datasets containing multiple rows of character vectors (like the one mentioned above). 现在,我想做的是,我有两个包含多个字符向量行的大型数据集(就像上面提到的那样)。 Now, I want to run this function parallely on both of them. 现在,我想同时在两个函数上运行此函数。 The datasets are of the form: 数据集的形式为:

1 2 "abcabcabc"
2 3 "adcadcadc"
3 4 "yufyufyuf"
4 5 "xyzxyzxyz"
..............

And similarly, with the first two columns being the same, and only the third column changing, I have, 同样,前两列相同,只有第三列发生变化,

1 2 "fbfbfbfbfb"
2 3 "bbfbfbfbbf"
3 4 "fbffffbfbf"
4 5 "fbfbbbbbbb"
...............

So, basically, on merging these two datasets, I will have, 因此,基本上,在合并这两个数据集时,我将拥有

1 2 "abcabcabc" "fbfbfbfbfb"
2 3 "adcadcadc" "bbfbfbfbbf"
3 4 "yufyufyuf" "fbffffbfbf"
4 5 "xyzxyzxyz" "fbfbbbbbbb"
...........................

Now, I want to run the function parallely on both the third and columns character vectors and store the output. 现在,我想在第三和第二个字符向量上并行运行该函数并存储输出。 How can I do it in R? 我如何在R中做到这一点?

Perhaps a data.table approach would be faster than trying to parallelize your code, but I would need a sample of your data to make sure this answer addresses your question 也许data.table方法比尝试并行化代码更快,但是我需要您的数据样本以确保此答案解决了您的问题

library(data.table)

cols <- c("colstring1", "colstring2")

setDT(data)[, (cols) := lapply (cols, function(x)  find.string(x) )]

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

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