从长格式转换到宽格式转换,是否有比 reshape 性能更好的解决方案?

Is there a solution with better performance than reshape from long to wide format conversion?

我有一小段代码可以将数据帧从长数据帧转换为宽数据帧。

library(reshape2)
mydata <- structure(list(issn = c("1980-4814", "1945-3116", "1681-4835", "1367-0751", "1516-6104", "1359-7566", "2319-0795", "1390-6615", "1808-8023", "1746-4269", "1852-2181", "0022-4596", "1808-2386", "0254-6051", "1981-3686", "1077-2618", "1809-3957", "2179-5746", "0147-6513", "1070-5503"), periodico = c("ABCustos (", "Journal of", "The Electr", "Logic Jour", "DIREITO, E", "REGIONAL &", "REVISTA FÓ", "UMBRAL: RE", "Segurança ", "Journal of", "Augm Domus", "Journal of", "BBR. Brazi", "Jinshu Rèc", "Revista Br", "IEEE Indus", "Revista SO", "Biota Amaz", "Ecotoxicol", "Internatio"), qualis = c("B4", "B3", "B2", "B2", "A1", "B5", "B5", "C ", "B5", "B3", "B3", "A1", "B4", "B3", "B5", "A2", "C ", "B3", "A2", "B1"), area = c(1L, 1L, 1L, 2L, 3L, 3L, 3L, 3L, 4L, 5L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 9L, 9L, 9L)), .Names = c("issn", "periodico", "qualis", "area"), row.names = c(1L, 501L, 1001L, 1501L, 2001L, 2501L, 3001L, 3501L, 4001L, 4501L, 5001L, 5501L, 6001L, 6501L, 7001L, 7501L, 8001L, 8501L, 9001L, 9501L), class = "data.frame")

reshape(mydata, direction = "wide", 
        idvar = c("issn", "periodico"), 
        timevar = "area")

数据

结果是

没问题,正是我想要的,但是随着数据帧超过 2.000 条记录,它变得非常慢。

我只有 10 个区域要映射到列,但有超过 10.000 个 issn。

我正在寻找更快的方法来实现相同的结果。

谢谢

对于重塑问题,data.table 中的 dcast 进行了高度优化并且非常高效,应该比当前可用的任何软件包都快

library(data.table)
dcast(setDT(mydata), issn+periodico~area, value.var = "qualis")

您可以为此使用 dplyrtidyr

library(dplyr)
library(tidyr)
mydata %>% 
  mutate(area = paste('qualis',area,sep=".")) %>% 
  spread(area, qualis)