考虑以下数据集:
dt <- structure(list(lllocatie = structure(c(1L, 6L, 2L, 4L, 3L), .Label = c("Assen", "Oosterwijtwerd", "Startenhuizen", "t-Zandt", "Tjuchem", "Winneweer"), class = "factor"),
lat = c(52.992, 53.32, 53.336, 53.363, 53.368),
lon = c(6.548, 6.74, 6.808, 6.765, 6.675),
mag.cat = c(3L, 2L, 1L, 2L, 2L),
places = structure(c(2L, 4L, 5L, 6L, 3L), .Label = c("", "Amen,Assen,Deurze,Ekehaar,Eleveld,Geelbroek,Taarlo,Ubbena", "Eppenhuizen,Garsthuizen,Huizinge,Kantens,Middelstum,Oldenzijl,Rottum,Startenhuizen,Toornwerd,Westeremden,Zandeweer", "Loppersum,Winneweer", "Oosterwijtwerd", "t-Zandt,Zeerijp"), class = "factor")),
.Names = c("lllocatie", "lat", "lon", "mag.cat", "places"),
class = c("data.table", "data.frame"),
row.names = c(NA, -5L))
当我想将最后一列中的字符串拆分为单独的行时,我使用(data.table
版本1.9.5+):
dt.new <- dt[, lapply(.SD, function(x) unlist(tstrsplit(x, ",", fixed=TRUE))), by=list(lllocatie,lat,lon,mag.cat)]
但是,当我使用:
dt.new2 <- dt[, lapply(.SD, function(x) unlist(tstrsplit(x, ",", fixed=TRUE))), by=lllocatie]
我得到了相同的结果,除了所有列都被强制转换为字符变量。问题在于,对于小型数据集,指定不必在参数中拆分的变量不是一个大问题by
,但对于具有许多列/变量的数据集,它是一个大问题。我知道可以使用splitstackshape
包来做到这一点(正如@ColonelBeauvel 在他的回答中提到的那样),但我正在寻找data.table
解决方案,因为我想将更多操作链接到此。
如果不手动指定不必在参数中拆分的变量,如何防止这种情况发生by
?