Search code examples
pythonrequivalentjustify

R: What's the R equivalent to numpy's .dtype.itemsize and .dtype.alignment array properties?


I'm trying to convert something like this from python into R:

dt = my_array.dtype
fw = int(dt.itemsize/dt.alignment)
b = numpy.array([list(w.ljust(fw)) for w in my_array.T])

I've looked around but haven't found anything on this particular topic.


Solution

  • The first line extracts the data type. R might use class(my_array). Using typeof or mode might also be possible but unless you have studied R for a while you may not get the information you desire. It appears that Python encodes several types of information in the datatype string. There isn't really an exact parallel in R but you might want to look at the value returned by str(). Unlike Python's dt, the value from str is not going to be accessible for further breaks=down by additional functions. From its help page:

    Value

    str does not return anything, for efficiency reasons. The obvious side effect is output to the terminal.

    The attributes function will sometimes yield additional information about an object, but in the case of an array there's nothing additional to the information from dim.

    > my_array <- array(1:24, c(2,3,4))  # a 2 x 3 x 4 array of integers
    > class(my_array)
    [1] "array"
    > str(my_array)
     int [1:2, 1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
    dim(my_array)   # Not sure, but this might be the equivalent of "alignment"
    [1] 2 3 4
     attributes(my_array)
    $dim
    [1] 2 3 4
    > length(my_array)
    [1] 24
    > mode(my_array)
    [1] "numeric"