我有一个独特的数据集(NYC MTA旋转门数据),我需要以某种方式重新组织以执行一些分析.我编写的代码有效,但效率不高,因为它是一个非常大的数据集.我希望有人能提出更好的方法.
有问题的数据集有43列.第1-3列是唯一标识符(即特定站点的旋转门).然后列4-8标识计量时间,计量类型,条目然后退出.然后,图9-13中的其余列最多为43,遵循相同的模式.数据集很难看,所以我不想在这里发布,但你可以在下面的链接中找到它.您将不得不查看10/18/14前的数据.
http://web.mta.info/developers/turnstile.html
#Vector of column numbers that identifies the break
a <- c(4, 9, 14, 19, 24, 29, 34, 39)
#The actual loop to re-sort the data
for (i in 1:nrow(data)) {
for (j in 1:length(a)) {
if (j == 8 ){ all <- rbind(all, cbind(data[i, 1:3], data[i, a[j]:43])) }
else { all <- rbind(all, cbind(data[i, 1:3], data[i,a[j]:(a[j+1]-1)])) } } }
Run Code Online (Sandbox Code Playgroud)
所有这一切的最终结果都是这样的.
1 2 3 1 2 3 4 5
5083 H026 R137 00-00-00 10-04-14 00:00:00 REGULAR 4072851 …Run Code Online (Sandbox Code Playgroud)