Error: Column indexes must be at most 1 if… heatmap.2

喜你入骨 提交于 2019-12-10 15:18:39

问题


I received an error in heatmap.2, and I found similar error here R : knnImputation Giving Error but it doesn't have an answer yet. I couldn't understand the problem. I am new to this world, sorry in advance if there is any mistake.

I have a dataframe df with 144 rows,177 columns which shows the monthly averages of years between 2005-2016 by timeAverage function openair package.

Here the small example from 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

remove non-numeric columns:

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

then I calculated distance matrix and produced dendograms. Finally, I used heatmap.2 to produce heatmap with dendograms.

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*")"))

However, when I run the code, it gives the error:

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

Any idea why this error occurs? Thanks in advance!


回答1:


  1. This link shows you how to do KNN another way: https://www.youtube.com/watch?v=u8XvfhBdbMw

  2. Also, I don't understand why the knnImputation(data) won't work - although I have played around with the data frame and now it does work - even though I don't know why it works now.

What I did was:

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 = There is now no missing data

Both work, but I'd also like to know from the experts why we got the error: column indexes must be at most 1 if positive etc.




回答2:


The primary explanation for the error reported can be found here.

I came into the problem today,and I found that we should transform our tbl object into data.frame object!!This is one disgusting point that different packages do not have compatibility.

#check your df class,I think your df is actually as tbl object
class(df)
df_new <- as.data.frame(df)


来源:https://stackoverflow.com/questions/45259649/error-column-indexes-must-be-at-most-1-if-heatmap-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!