如何使用dplyr加速行级操作

时间:2017-02-20 12:02:35

标签: r dplyr

我有一个大型数据集,我尝试使用dplyr进行操作。我的数据争用任务需要行级别字符串操作。

我使用默认的rowwise()函数,代码正常运行。但是,该操作需要花费大量时间才能完成。

VR_vehicle_GPSLocation = c("12.36556|0.74518153|xxxxxxxxxx", 
      "-51.75810|165.55526|xxxxxxxxxx", 
      "GPS nicht verfügbar",
      "48.77410|171.08364|xxxxxxxxxx", 
      "GPS Not Available",
      "0|0|N/R",
      "32.18661| 170.56615|xxxxxxxxxx")
df = data.frame(VR_vehicle_GPSLocation)

jobs_location <- df %>%
     rowwise() %>% 
     mutate(latitude  = as.numeric(unlist(strsplit(as.character(VR_vehicle_GPSLocation), split='\\|'))[1]),
            longitude = as.numeric(unlist(strsplit(as.character(VR_vehicle_GPSLocation), split='\\|'))[2])) %>%
     select(latitude, longitude)

为了加快这个过程,我探索了multidyplyr库但没有成功,我收到一条错误消息,说我的数据集不是数据框。

jobs_location <- jobs %>%
  partition() %>%
  rowwise() %>% 
  mutate(latitude  = as.numeric(unlist(strsplit(as.character(VR_vehicle_GPSLocation), split='\\|'))[1]),
         longitude = as.numeric(unlist(strsplit(as.character(VR_vehicle_GPSLocation), split='\\|'))[2])) %>%
  collect()

1 个答案:

答案 0 :(得分:2)

归功于@DavidArenburg

我从一个非有效的角度来解决这个问题。使用矢量化方法可以明显改善性能。

为了完整起见,我在整个数据集的随机微小子集上运行代码来评估性能,并且清晰的矢量化是我解决问题的方法。

最后,重要的是要提到需要预清洁任务以确保最终的转换是数字的(有关更多详细信息,请参阅David的评论)

pgc++ 16.10-0 64-bit target on x86-64 Linux -tp sandybridge

一张图片胜过千言万语

enter image description here

David的另一个建议是在library(dplyr) library(data.table) library(microbenchmark) library(ggplot2) mbm = microbenchmark( a = sample_n(jobs, 100) %>% rowwise() %>% mutate(latitude = as.numeric(unlist(strsplit(as.character(VR_vehicle_GPSLocation), split='\\|'))[1]), longitude = as.numeric(unlist(strsplit(as.character(VR_vehicle_GPSLocation), split='\\|'))[2])), b = setDT(sample_n(jobs, 100))[grep("|", VR_vehicle_GPSLocation, fixed = TRUE), c("latitude", "longitude") := tstrsplit(VR_vehicle_GPSLocation, "|", fixed = TRUE, keep = 1:2, type.convert = TRUE)] ) autoplot(mbm) 之后将数据转换为数字。我添加了两个函数,一个是在整个列上进行转换,另一个是在拆分后应用类型转换。

tstrsplit

最后一个变种(d)显然是赢家。

enter image description here