row names supplied are of the wrong length in R -
i running r program computes similarity between product descriptions. input program file 1 column, containing list of product descriptions, each on separate row
i have file contains list of product titles, each on separate row.
using dist function, have computed similarity between product descriptions , stored in dist.mat matrix.
next, want join product title similarity have computed. so, read product titles in names , then:
dist.mat <- data.frame(dist.mat, row.names=names[,1]) colnames(dist.mat) <- (row.names(dist.mat))
and error: error in data.frame(dist.mat, row.names = names[, 1]) : row names supplied of wrong length
not sure on how fix it. read this: invalid 'row.names' length can't fix error using sample$ or as.character
i using: lsa_0.73, snowballc_0.5.1, tm_0.5-10
here actual example: product desc file:
- this glass can used drink whiskey
- this stainless steel glass
- this red rose
product title file:
- whiskeyglass
- glass
- rose
would great if can help
distance matrix (class dist
) vector displayed 1 row , 1 column smaller triangular matrix vector length.
library(stringdist) desc <- c("this glass can used drink whiskey", "this stainless steel glass", "this red rose") names <- c("whiskeyglass", "glass", "rose") dist.mat1 <- stringdistmatrix(desc) dist.mat1 # 1 2 # 2 27 # 3 24 18
however, dist
object not have dimensions , therefore row , column names cannot assigned it.
dim(dist.mat1) # null
trying name rows , columns of dist
object results in error.
row.names(dist.mat1) <- colnames(dist.mat1) <- names
error in as.data.frame.default(x[[i]], optional = true) : cannot coerce class ""dist"" data.frame
to obtain result expect, dist
object first needs converted matrix
. adds zeros along diagonal , row , column.
if(class(dist.mat1) == "dist"){ dist.mat2 <- as.matrix(dist.mat1) row.names(dist.mat2) <- colnames(dist.mat2) <- names } else { dist.mat2 <- dist.mat1 row.names(dist.mat2) <- colnames(dist.mat2) <- names } dist.mat2 # whiskeyglass glass rose # whiskeyglass 0 27 24 # glass 27 0 18 # rose 24 18 0
if dist.mat
looks dist.mat1
above, class matrix
, need select names belong where.
row.names(dist.mat) <- names[-1] # removing first name rows colnames(dist.mat) <- names[-length(names)] # removing last name columns