Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

labeling row and col names when using dist() and as.matrix()

I was wondering if there is a specific parameter to output a table with all the row names when using dist() and as.matrix(). Here's what I mean:

first=c('john', 'judy', 'jenny')
second=c(3,6,9)
third = c(2,4,6)
df = data.frame(first,second,third)

I have this data frame called df:

 first second third
1  john      3     2
2  judy      6     4
3 jenny      9     6

Here's my desired output:

      john       judy   jenny
john 0.000000 4.41588 8.831761
judy 4.415880 0.00000 4.415880
jenny 8.831761 4.41588 0.000000

This is my code:

df.dist=dist(df)
df.dist=as.matrix(df.dist, labels=TRUE)
df.dist

And Here's what R is giving me:

      1       2        3
1 0.000000 4.41588 8.831761
2 4.415880 0.00000 4.415880
3 8.831761 4.41588 0.000000

I was wondering if there is a specific function or parameter that renames the columns when comparing different entries, or do we just need to code that ourselves?

Another thing that I saw when I typed ?as.matrix is that there is a param called dimnames that lets you input list of names for cols and rows. But I don't know if this would be such a good idea since my dataset has about 100+ entries.

Any help is deeply appreciated. Been stuck for a while.

like image 282
jason adams Avatar asked Nov 24 '14 21:11

jason adams


People also ask

How do you name columns and rows of a matrix in R?

Naming Rows and Columns of a Matrix in R Programming – rownames() and colnames() Function. rownames() function in R Language is used to set the names to rows of a matrix.

How do you name columns in a matrix?

We use colnames() function for renaming the matrix column in R. It is quite simple to use the colnames() function. If you want to know more about colnames() function, then you can get help about it in R Studio using the command help(colnames) or ? colnames().

Can matrix have column names?

You can assign names to the rows and columns of a matrix with the rownames and colnames functions.

What is use of row names ()?

The rownames() and colnames() functions in R are used to obtain or set the names of the row and column of a matrix-like object, respectively.


Video Answer


2 Answers

It is only one line of code to add these names as row and column names:

df<-read.table(header=T,text='first second third
1 john      3     2
2 judy      6     4
3 jenny      9     6')

df.dist=dist(df)
df.dist=as.matrix(df.dist, labels=TRUE)
colnames(df.dist) <- rownames(df.dist) <- df[['first']] #this is the only line

> df.dist
          john    judy    jenny
john  0.000000 4.41588 8.831761
judy  4.415880 0.00000 4.415880
jenny 8.831761 4.41588 0.000000

dimnames adds the names as attributes so you might be better off with the above.

like image 132
LyzandeR Avatar answered Sep 18 '22 23:09

LyzandeR


You can also set the first column as the data frame rownames, then use dist:

rownames(df) <- df$first
as.matrix(dist(df[-1]))

#          john     judy    jenny
#john  0.000000 3.605551 7.211103
#judy  3.605551 0.000000 3.605551
#jenny 7.211103 3.605551 0.000000
like image 24
Psidom Avatar answered Sep 19 '22 23:09

Psidom