problem in understanding a variable in R language

Hi. I'm very beginner in R language. I'm reading this link Multiple Data (Time Series) Streams Clustering – Peter Laurinec – Time series data mining in R. Bratislava, Slovakia. and I confronted with the following expression:

res_clust <- kmeans(data_ave_prof, 12, nstart = 20)
data_centroid <- data.table(melt(res_clust$centers))
data_centroid[, Clust := Var1]

I appreciate if anyone can help me what is the meaning of Var1 in data_centroid[, Clust := Var1]? and does Clust mean to create a new column for data_centroids which is a matrix with the name Clust?

Hi,

Welcome to the RStudio community!

I do not use the data.table package myself, so this was confusing to me too, but what is happening is that the Var1 is a result from the melt() function performed on the dataset. To make it more confusing, data.table has a melt function, but the one used here is from the reshape2 package, which will be used if installed.

When a table is melted (i.e., becomes long format) the row numbers become Var1 and the column names Var2. Then with the special assignment := used in data.table the Var1 column is duplicated and renamed Clust.

library(data.table)

#Generate some data
set.seed(1) #Only needed for reproducibility 
myData = data.frame(
  x = runif(1:10),
  y = runif(1:10)
)

#Run k-means
test = kmeans(myData, 3)

#Show centers table
test$centers
#>           x         y
#> 1 0.8450967 0.4948642
#> 2 0.6168256 0.8394645
#> 3 0.2252752 0.4824545

#Melt table
test = data.table(reshape2::melt(test$centers))
test
#>    Var1 Var2     value
#> 1:    1    x 0.8450967
#> 2:    2    x 0.6168256
#> 3:    3    x 0.2252752
#> 4:    1    y 0.4948642
#> 5:    2    y 0.8394645
#> 6:    3    y 0.4824545

#Duplicate and rename column Var1 to Clust
test[,Clust := Var1]
test
#>    Var1 Var2     value Clust
#> 1:    1    x 0.8450967     1
#> 2:    2    x 0.6168256     2
#> 3:    3    x 0.2252752     3
#> 4:    1    y 0.4948642     1
#> 5:    2    y 0.8394645     2
#> 6:    3    y 0.4824545     3

Created on 2022-06-01 by the reprex package (v2.0.1)

Hope this helps,
PJ

1 Like

Thank you so much it was so helpful

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.