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.
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.
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().
You can assign names to the rows and columns of a matrix with the rownames and colnames functions.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With