R中的dist只能处理数据帧、向量、矩阵和列表

dist in R can only handle data frames, vectors, matrices, and lists

我正在尝试 运行 此代码从 UCR Time Series database 获取枪点数据集的层次聚类。但是我遇到了这个错误。有帮助吗?

library(dtw)
set.seed(6218)
n <- 10
sc <- read.table("/Users/mona/cs799/UCR_TS_Archive_2015/Gun_Point/Gun_Point_TRAIN")
s <- sample(1:100, n)
idx <- c(s, 100+s, 200+s, 300+s, 400+s, 500+s)
sample2 <- sc[idx, ]
distance.matrix <- dist(sample2, method="DTW")
hc <- hclust(distance.matrix, method="average")
plot(hc, labels=observedLabels, main="")
rect.hclust(hc, k=6)
memb <- cutree(hc, k=6)
table(observedLabels, memb)

这是错误:

Error in dist(sample2, method = "DTW") : 
  Can only handle data frames, vectors, matrices, and lists!

这是额外的信息:

> class(sample2)
[1] "factor"
> head(sample2)
[1] 1,-1.162,-1.1614,-1.1624,-1.1641,-1.1615,-1.1578,-1.1595,-1.1614,-1.1587,-1.1616,-1.1586,-1.1539,-1.1553,-1.1563,-1.1577,-1.1372,-1.0873,-1.0076,-0.92273,-0.83391,-0.77351,-0.75743,-0.74978,-0.74081,-0.74768,-0.74807,-0.74087,-0.7363,-0.73648,-0.73697,-0.73272,-0.72451,-0.71322,-0.70734,-0.69868,-0.69435,-0.66939,-0.61584,-0.50646,-0.36608,-0.22283,-0.047168,0.1419,0.29323,0.42329,0.55716,0.68035,0.78691,0.89422,0.97334,1.0395,1.0935,1.1428,1.1796,1.2073,1.213,1.2158,1.2299,1.2299,1.2338,1.2341,1.2427,1.2579,1.2583,1.2487,1.2571,1.2516,1.2525,1.2576,1.2408,1.2475,1.2464,1.245,1.2468,1.2526,1.2384,1.2384,1.2441,1.2349,1.2412,1.235,1.2393,1.2369,1.2252,1.2236,1.2212,1.2271,1.223,1.2367,1.2328,1.2265,1.2274,1.2269,1.2171,1.2092,1.2075,1.1895,1.1762,1.1432,1.1067,1.0547,0.98044,0.89041,0.77871,0.65819,0.50315,0.34939,0.18367,0.043842,-0.090519,-0.21244,-0.33155,-0.44732,-0.54271,-0.6227,-0.68627,-0.72709,-0.76162,-0.77914,-0.79041,-0.79569,-0.79752,-0.799,-0.79718,-0.80404,-0.8125,-0.81943,-0.82027,-0.82311,-0.82287,-0.81811,-0.81731,-0.82755,-0.84642,-0.86898,-0.90457,-0.95582,-1.0169,-1.073,-1.1026,-1.1276,-1.1367,-1.1378,-1.1325,-1.1294,-1.1282,-1.1282,-1.1258,-1.1214,-1.1162                                                                        
[2] 1,-1.1772,-1.1758,-1.1732,-1.1709,-1.1695,-1.1663,-1.1659,-1.1676,-1.1669,-1.1684,-1.1687,-1.1699,-1.172,-1.1745,-1.1722,-1.151,-1.1112,-1.0381,-0.96358,-0.87919,-0.81776,-0.77833,-0.76375,-0.75816,-0.76006,-0.74471,-0.72135,-0.70109,-0.68857,-0.6813,-0.67462,-0.6731,-0.67252,-0.6547,-0.60799,-0.54051,-0.41217,-0.2532,-0.098128,0.079075,0.22602,0.379,0.50076,0.628,0.73885,0.8542,0.94419,1.025,1.0879,1.1311,1.1609,1.1705,1.1872,1.1884,1.1893,1.1937,1.2015,1.2024,1.2009,1.199,1.2022,1.1991,1.1992,1.2007,1.1984,1.2002,1.1974,1.1972,1.1973,1.1948,1.1926,1.1914,1.1898,1.1863,1.1923,1.1894,1.1894,1.1914,1.1917,1.1896,1.1881,1.191,1.1917,1.1914,1.1959,1.1929,1.1937,1.185,1.1886,1.1872,1.1825,1.181,1.1789,1.1728,1.1659,1.1553,1.1236,1.0979,1.0542,0.98838,0.90653,0.81815,0.70866,0.59793,0.48555,0.37193,0.25358,0.14504,0.030856,-0.07891,-0.17755,-0.28434,-0.37343,-0.47522,-0.55537,-0.63135,-0.69797,-0.75798,-0.79107,-0.81301,-0.82609,-0.83507,-0.83829,-0.83203,-0.8294,-0.82387,-0.82324,-0.8209,-0.8196,-0.82809,-0.83646,-0.84388,-0.85982,-0.88116,-0.90654,-0.93195,-0.96394,-1.0157,-1.0819,-1.1516,-1.2256,-1.2957,-1.3274,-1.3271,-1.3004,-1.2711,-1.2673,-1.265,-1.2707,-1.2621                                                                                    
[3] <NA>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
[4] 2,-0.60329,-0.60738,-0.60683,-0.60757,-0.60495,-0.59613,-0.59006,-0.58622,-0.58376,-0.58308,-0.58165,-0.58027,-0.57905,-0.57758,-0.57233,-0.57126,-0.56686,-0.56655,-0.56238,-0.56232,-0.56109,-0.56225,-0.56121,-0.56103,-0.56126,-0.5647,-0.56771,-0.57546,-0.58215,-0.58791,-0.59477,-0.60212,-0.60924,-0.6135,-0.61131,-0.61516,-0.61299,-0.61363,-0.61023,-0.60687,-0.59831,-0.58668,-0.57709,-0.56536,-0.55586,-0.54901,-0.5392,-0.53329,-0.5288,-0.51145,-0.49628,-0.47031,-0.43497,-0.36743,-0.29702,-0.23969,0.06958,0.25768,0.43618,0.57948,0.7313,0.90601,1.1171,1.3602,1.6123,1.7955,1.8947,1.9255,1.9508,1.9576,1.9752,1.9944,1.999,2.0095,2.0169,2.0169,2.0169,2.02,2.0228,2.0267,2.0298,2.0268,2.0232,2.0187,2.0114,1.9897,1.9277,1.8376,1.6944,1.5212,1.3611,1.2183,1.0752,0.95557,0.83104,0.68869,0.52236,0.49685,0.30739,0.033138,-0.18204,-0.33481,-0.47059,-0.61725,-0.73227,-0.80944,-0.85854,-0.87032,-0.86165,-0.83881,-0.80939,-0.79222,-0.77832,-0.75982,-0.74043,-0.71732,-0.69354,-0.67625,-0.66151,-0.64793,-0.6419,-0.63702,-0.63379,-0.63214,-0.62507,-0.62084,-0.61382,-0.6043,-0.59542,-0.58973,-0.58362,-0.57583,-0.57109,-0.56928,-0.56997,-0.57433,-0.58042,-0.59036,-0.59354,-0.59619,-0.59676,-0.59451,-0.5956,-0.60355,-0.61221,-0.62352,-0.62531,-0.62541,-0.6147,-0.60478
[5] <NA>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
[6] <NA>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
50 Levels: 1,-0.60447,-0.60808,-0.61307,-0.61493,-0.61552,-0.6163,-0.61799,-0.61882,-0.6199,-0.62087,-0.62352,-0.62598,-0.63053,-0.63174,-0.6335,-0.63514,-0.63936,-0.63967,-0.64228,-0.6454,-0.64922,-0.65482,-0.65691,-0.65971,-0.66131,-0.66389,-0.66499,-0.6666,-0.66699,-0.67166,-0.67122,-0.67076,-0.66669,-0.6416,-0.5998,-0.56133,-0.54458,-0.54162,-0.54626,-0.5583,-0.56274,-0.55377,-0.53691,-0.52087,-0.51263,-0.50752,-0.47041,-0.38747,-0.24086,-0.057702,0.14533,0.39479,0.62701,0.83947,1.0471,1.2491,1.4651,1.6728,1.8217,1.8899,1.8993,1.9261,1.9342,1.9443,1.9543,1.9587,1.972,1.9716,1.9754,1.981,1.9813,1.9838,1.9872,1.9876,1.9872,1.9775,1.9775,1.9531,1.9311,1.8955,1.8364,1.7441,1.641,1.4992,1.343,1.1789,1.014,0.85102,0.69553,0.53011,0.3696,0.21567,0.07815,-0.061246,-0.19735,-0.31344,-0.39569,-0.46799,-0.51703,-0.54917,-0.56338,-0.56668,-0.56711,-0.57036,-0.56802,-0.56587,-0.56708,-0.56863,-0.56555,-0.55769,-0.54395,-0.52459,-0.49385,-0.45891,-0.43498,-0.4228,-0.43615,-0.46825,-0.5193,-0.57468,-0.63775,-0.70106,-0.7464,-0.78227,-0.79285,-0.79368,-0.79098,-0.78938,-0.78593,-0.7767,-0.76303,-0.74037,-0.7158,-0.68908,-0.66368,-0.64775,-0.643,-0.6412,-0.64076,-0.64373,-0.64642,-0.64726,-0.64674,-0.64507,-0.64393,-0.64289,-0.64251,-0.64078,-0.64053,-0.64021 ...
> 

您需要将 read.table 更改为:

sc <- read.table("~/cs799/UCR_TS_Archive_2015/Gun_Point/Gun_Point_TRAIN", 
                 sep=",", stringsAsFactors=FALSE)

那么,您将至少拥有有效的数据来进行操作。

但是,sc 只有 50 行,idx 显然超出了某些值的行数(它生成了大量 NA)。

而且,如果其他人遇到此 post 和来自 UCR 时间序列分类档案(此处 http://www.cs.ucr.edu/~eamonn/time_series_data/UCR_TS_Archive_2015.zip)的此数据集,密码为 attempttoclassify。我害怕寻宝发现它并不像把它放在一起的学者所希望的那样可爱。