Underlying C and Fortran Code in kmeans function

Hello All,

I'm trying to take a peak under the hood of the base r function kmeans but I can't seem to find the source code for some of the underlying functions. Namely:

The fortran function "C_kmns"
The C functions : C_kmens_Lloyd and C_kmeans_MacQueen

How/where should I look for the code of these functions?

Thank you.

> getAnywhere(C_kmeans_Lloyd)
A single object matching ‘C_kmeans_Lloyd’ was found
It was found in the following places
  namespace:stats
with value

$name
[1] "kmeans_Lloyd"

$address
<pointer: 0x0000024ae68344a0>
attr(,"class")
[1] "RegisteredNativeSymbol"

$dll
DLL name: stats
Filename: C:/Program Files/R/R-3.6.2/library/stats/libs/x64/stats.dll
Dynamic lookup: FALSE

$numParameters
[1] 9

attr(,"class")
[1] "CRoutine"         "NativeSymbolInfo"

my interpretation is that we are only provided a compiled routine that lives in the stats.dll
I suppose you can try to contact the R foundation for such details ?
https://www.r-project.org/foundation/

1 Like

Thank you, I will reach out to them.

There's a mirror of the R source code on github and this looks like the underlying kmeans code.

For future reference, here's a tutorial by Jenny Bryan on accessing R source code.

For functions that are called with .Internal() or .Primitive(), pryr::show_c_source will take you right to the function code. For example, running

pryr::show_c_source(.Internal(mean(x)))

will take you to https://github.com/search?q=SEXP%20attribute_hidden%20do_summary+repo:wch/r-source&type=Code. Unfortunately, this doesn't work for kmeans. I'm guessing this is because, as the output of getAnywhere(kmeans) indicates, kmeans_Lloyd is called with .C().

2 Likes

Thank you Joels!
Now I just need to find the source code to C_kmns

Is this the one you mean? It's actually in Fortran, but inspection of the output of getAnywhere(kmeans) indicates that kmns is a Fortran function (see the line Z <- .Fortran(C_kmns, x, m, p, centers = centers,).

I found it by going to the R source code mirror on github and typing kmns in the search box. If you scroll down through the results, you'll see a file called kmns.f, which is the one linked above.

1 Like

Yes! that is what I was look for, thank you joels!

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