我在heatmap.2中收到错误,我在R : knnImputation Giving Error发现了类似的错误,但它还没有答案。我无法理解这个问题。我是这个世界的新手,如果有任何错误,请提前抱歉。
我的数据框df
包含144行,177列,显示2005-2016
到timeAverage
函数openair package
之间的月平均值。
这是df
的小例子:
date Year Month Adana-Catalan Adana-Dogankent Adana-Meteoroloji
2008/09/01 2008 9 NaN NaN NaN
2008/10/01 2008 10 NaN NaN 1.7948718
2008/11/01 2008 11 NaN NaN 2.0909091
2008/12/01 2008 12 1.2694064 12.2384106 0.7272727
2009/01/01 2009 1 2.3150358 12.7479339 10.3779762
2009/02/01 2009 2 2.8241107 18.4320175 2.4494949
2009/03/01 2009 3 2.0401606 8.4597523 1.6529412
2009/04/01 2009 4 1.8604651 4.8560000 1.1267606
2009/05/01 2009 5 2.1087719 1.8202247 NaN
2009/06/01 2009 6 4.0695103 2.1463415 1.1111111
2009/07/01 2009 7 5.4016393 8.1298905 NaN
2009/08/01 2009 8 0.1313869 16.9874411 NaN
2009/09/01 2009 9 NaN 5.3753943 NaN
2009/10/01 2009 10 1.6626506 8.8000000 1.8388889
2009/11/01 2009 11 1.4177632 NaN 3.9879154
2009/12/01 2009 12 0.9644128 NaN 5.0281457
2010/01/01 2010 1 0.2608696 4.0898876 3.1981424
2010/02/01 2010 2 0.7619048 NaN 4.3169811
删除非数字列:
df.monthly <- df[,-c(1:3)] #remove non-numeric columns
df.monthly.un <- unlist(df.monthly) #unlist the list
df.monthly.un[is.nan(df.monthly.un)] <- -999 #replace NaNs with -999
monthly.dim <- dim(df.monthly)
monthly.frame <- matrix(df.monthly.un, monthly.dim) #convert unlist to matrix
然后我计算了距离矩阵并产生了树状图。最后,我使用heatmap.2
生成带有树状图的热图。
monthly.dist <- dist(monthly.frame)
monthly.hclust <- hclust(monthly.dist, method="complete")
monthly.dist2 <- dist(t(monthly.frame))
colClust <- as.dendrogram(hclust(monthly.dist2, method="complete"))
rowClust <- as.dendrogram(monthly.hclust)
colpalette <- colorRampPalette(c("red","blue","green"))(n=100)
heatmap.2(monthly.frame, scale="none",
col=colpalette, trace= "none", cexRow=0.6, cexCol=1,
cex.main=0.7, key=T, Rowv=rowClust, labRow=df[,1],
main=expression("2005-2016 SO"[2] * " (ug/m"^3*")"))
但是,当我运行代码时,它会给出错误:
Error: Column indexes must be at most 1 if positive, not 22, 23, 24, 25, 21, 18, 19, 20, 16, 17, 12, 10, 11, 15, 13, 14, 3, 9, 8, 4, 7, 5, 6, 2, 124, 125, 121, 122, 123, 133, 132, 131, 134, 135, 126, 129, 127, 128, 130, 136, 137, 143, 144, 141, 142, 138, 139, 140, 57, 58, 55, 56, 42, 47, 41, 40, 36, 38, 37, 39, 46, 43, 44, 45, 34, 35, 26, 27, 28, 29, 30, 31, 32, 33, 59, 54, 53, 48, 49, 50, 51, 112, 116, 117, 114, 115, 88, 89, 52, 60, 63, 70, 75, 73, 74, 79, 77, 76, 78, 66, 67, 62, 65, 71, 64, 61, 72, 97, 87, 85, 86, 90, 98, 91, 83, 84, 92, 94, 96, 93, 95, 68, 69, 82, 80, 81, 113, 110, 111, 109, 118, 119, 120, 101, 105, 103, 104, 99, 106, 100, 102, 107, 108
知道为什么会出现这种错误吗?提前谢谢!
答案 0 :(得分:1)
此链接向您展示如何以另一种方式进行KNN: https://www.youtube.com/watch?v=u8XvfhBdbMw
此外,我不明白为什么knnImputation(数据)不会起作用 - 虽然我已经玩过数据框,但现在它确实有效 - 尽管我没有&#39;我知道它为什么现在有效。
我做的是:
mydata <- read_excel("icecreamdata.xlsx") #Here I'm importing my data
newdata <- data.frame() #I've created a blank data frame
newdata <- data.frame(mydata) #I'm putting the data I want into this new data frame
anyNA(newdata) #Checking for missing data. TRUE = yes, data is missing. FALSE = no, data is not missing.
fixeddata <- knnImputation(newdata) #Imputing data to a new object
anyNA(fixed data)
FALSE =现在没有遗漏数据
两者都有效,但我也想知道专家为什么会收到错误:如果是正数,列索引必须至多为1。
答案 1 :(得分:0)
The primary explanation for the error reported can be found here.
我今天遇到了问题,我发现我们应该将tbl对象转换为data.frame对象!!这是令人讨厌的一点,即不同的软件包没有兼容性。
~/.bashrc