如何避免大数据集的慢循环?

时间:2017-08-01 00:24:00

标签: r dataframe large-data

考虑这个数据集:

> DATA <- data.frame(Agreement_number = c(1,1,1,1,2,2,2,2),
+                    country = c("Canada","Canada", "USA", "USA", "Canada","Canada", "USA", "USA"), 
+                    action = c("signature", "ratification","signature", "ratification", "signature", "ratification","signature", "ratification"), 
+                    signature_date = c(2000,NA,2000,NA, 2001, NA, 2002, NA),
+                    ratification_date = c(NA, 2001, NA, 2002, NA, 2001, NA, 2002))
> DATA
Agreement_number country       action signature_date ratification_date
              1  Canada    signature           2000                NA
             1  Canada ratification             NA              2001
             1     USA    signature           2000                NA
             1     USA ratification             NA              2002
             2  Canada    signature           2001                NA
             2  Canada ratification             NA              2001
             2     USA    signature           2002                NA
             2     USA ratification             NA              2002

如您所见,一半的行有重复的信息。对于像这样的小数据集,删除重复数据非常容易。我可以使用coalesce函数(dplyr package),删除“action”列,然后删除所有不相关的行。虽然,还有很多其他方法。最终结果应如下所示:

> DATA <- data.frame( Agreement_number = c(1,1,2,2),
+                     country = c("Canada", "USA", "Canada","USA"), 
+                     signature_date = c(2000,2000,2001,2002),
+                     ratification_date = c(2001, 2002, 2001, 2002))
> DATA
Agreement_number country signature_date ratification_date
             1  Canada           2000              2001
             1     USA           2000              2002
             2  Canada           2001              2001
             2     USA           2002              2002

问题是,我的真实数据集更大(102000 x 270)并且还有更多变量。实际数据也更不规则,缺少更多值。 coalesce函数似乎很慢。到目前为止我能做的最好的循环仍然需要5-10分钟才能运行。

有一种简单的方法可以做得更快吗?我觉得R中必须有一些功能用于那种操作,但我找不到任何功能。

3 个答案:

答案 0 :(得分:4)

我认为你需要dcastdata.table库中的版本称自己为“快速”,根据我的经验,它在大型数据集上的速度很快。

首先,让我们创建一个signature_dateratification_date列,具体取决于操作

library(data.table)
setDT(DATA)[, date := ifelse(action == "ratification", ratification_date, signature_date)]

现在,让我们将其转换为操作是列,值是日期

wide <- dcast(DATA, Agreement_number + country ~ action, value.var = 'date')

如此宽阔的样子

  Agreement_number country ratification signature
1                1  Canada         2001      2000
2                1     USA         2002      2000
3                2  Canada         2001      2001
4                2     USA         2002      2002

答案 1 :(得分:3)

OP告诉他,他的生产数据有100行×270列,速度是他关注的问题。因此,我建议使用data.table

我知道Harland也建议使用data.tabledcast(),但下面的解决方案是另一种方法。它以正确的顺序显示行,并将ratification_date复制到签名行。经过一些清理后,我们得到了理想的结果。

library(data.table)

# coerce to data.table,
# make sure that the actions are ordered properly, not alphabetically
setDT(DATA)[, action := ordered(action, levels = c("signature", "ratification"))]

# order the rows to make sure that signature row and ratification row are
# subsequent for each agreement and country
setorder(DATA, Agreement_number, country, action)

# copy the ratification date from the row below but only within each group
result <- DATA[, ratification_date := shift(ratification_date, type = "lead"), 
                by = c("Agreement_number", "country")][
                  # keep only signature rows, remove action column
                  action == "signature"][, action := NULL]
result
   Agreement_number country signature_date ratification_date dummy1 dummy2
1:                1  Canada           2000              2001      2      D
2:                1     USA           2000              2002      3      A
3:                2  Canada           2001              2001      1      B
4:                2     USA           2002              2002      4      C

数据

OP已经提到他的生产数据有270列。为了模拟这个,我添加了两个虚拟列:

set.seed(123L)
DATA <- data.frame(Agreement_number = c(1,1,1,1,2,2,2,2),
country = c("Canada","Canada", "USA", "USA", "Canada","Canada", "USA", "USA"), 
action = c("signature", "ratification","signature", "ratification", "signature", "ratification","signature", "ratification"), 
signature_date = c(2000,NA,2000,NA, 2001, NA, 2002, NA),
ratification_date = c(NA, 2001, NA, 2002, NA, 2001, NA, 2002),
dummy1 = rep(sample(4), each = 2L),
dummy2 = rep(sample(LETTERS[1:4]), each = 2L))

请注意,set.seed()用于抽样时的可重复结果。

  Agreement_number country       action signature_date ratification_date dummy1 dummy2
1                1  Canada    signature           2000                NA      2      D
2                1  Canada ratification             NA              2001      2      D
3                1     USA    signature           2000                NA      3      A
4                1     USA ratification             NA              2002      3      A
5                2  Canada    signature           2001                NA      1      B
6                2  Canada ratification             NA              2001      1      B
7                2     USA    signature           2002                NA      4      C
8                2     USA ratification             NA              2002      4      C

附录:dcast()附加列

Harland建议使用data.tabledcast()。除了答案中的其他几个缺陷外,它还没有处理OP提到的额外列。

下面的dcast()方法还会返回其他列:

library(data.table)

# coerce to data table
setDT(DATA)[, action := ordered(action, levels = c("signature", "ratification"))]

# use already existing column to "coalesce" dates
DATA[action == "ratification", signature_date := ratification_date]
DATA[, ratification_date := NULL]

# dcast from long to wide form, note that ... refers to all other columns
result <- dcast(DATA, Agreement_number + country + ... ~ action, 
                value.var = "signature_date")
result
   Agreement_number country dummy1 dummy2 signature ratification
1:                1  Canada      2      D      2000         2001
2:                1     USA      3      A      2000         2002
3:                2  Canada      1      B      2001         2001
4:                2     USA      4      C      2002         2002

请注意,此方法将更改列的顺序。

答案 2 :(得分:2)

这是使用uwe-block的data.frame的另一个data.table解决方案。它类似于uwe-block的方法,但使用max来折叠数据。

# covert data.frame to data.table and factor variables to character variables
library(data.table)
setDT(DATA)[, names(DATA) := lapply(.SD,
                                    function(x) if(is.factor(x)) as.character(x) else x)]

# collapse data set, by agreement and country. Take max of remaining variables.
 DATA[, lapply(.SD, max, na.rm=TRUE), by=.(Agreement_number, country)][,action := NULL][]

lapply运行未包含在by语句中的变量,并在删除NA值后计算最大值。链中的下一个链接删除不需要的操作变量,最后(不必要的)链接打印输出。

返回

   Agreement_number country signature_date ratification_date dummy1 dummy2
1:                1  Canada           2000              2001      2      D
2:                1     USA           2000              2002      3      A
3:                2  Canada           2001              2001      1      B
4:                2     USA           2002              2002      4      C