在 R 中应用 forloop 函数
我正在为两个矩阵运行双 forloop。但一个矩阵大约有 90,000 行。 在 R 中它太慢。所以,如果可能的话,我想为此采用 apply 函数。
- 一个矩阵有 90,000 X 1 列,每行包含字符串信息。例如 1 行值(ID)AAAA12
- 另一个矩阵也有大约 90,000 但略多于 90,000 X 2 列,因此对于一行(ID),第一列有 AAAA23 以及相应的月份信息,例如 AAAA23 Jan 第二行,AAAA12 Feb ...等
所以,我想将一列匹配的月份信息从第二个矩阵合并到第一个矩阵。
输出矩阵的第一行将是 AAAA12 Feb,而不是使用 for 循环,如何快速生成这样的矩阵?
任何输入都会有帮助。
I am running double forloop for two matrices. but one matrix has around 90,000 rows.
it is too slow in R. so, I would like to take apply function for this if possible.
- One matrix has 90,000 X 1 column with string information per row. e.g 1row value(ID) AAAA12
- The other matrix has also around 90,000 but a bit more than 90,000 X 2 columns, so for one row(ID) has AAAA23 in 1st column and corresponding month information e.g AAAA23 Jan
And 2nd row, AAAA12 Feb ...etc
So, I would like to merge one column of matched month information from 2nd matrix to 1st mat.
1st row of output mat is going to be AAAA12 Feb. instead of using for loop, how can I generate such a matrix quickly?
Any input will be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的代码可能会解决这个问题:
这给了你
......然后在大约 90000 行上计时显示它需要大约 0.04 秒:
The following might do the trick:
Which gives you
...And then timing it on around 90000 rows shows it takes around 0.04 seconds:
apply
不会比for
循环快。您想要合并
或匹配
。apply
will be no faster than afor
-loop. You wantmerge
ormatch
.