Search code examples
rloggingtransformcube

Taking cube root and log transformation in R


I have a table with row names corresponding to a set of people and their corresponding body mass estimates. For instance, say a matrix "mass estimate" with these values:

   Name      Mass
1 person_a   234
2 person_b   190
3 person_c   203
4 person_d   176

How will I, in a single line of R code, take the cube roots of the masses and then have them log transformed?

I am not sure how to ask the data above in a table format, since the final question shows it on a single line. The first column reads "Name" and the second column reads "Mass". Each row has a name (person_a) and the mass (234).

Thanks!


Solution

  • # Sample matrix
    mat <- matrix(runif(20), ncol = 5);
    
    # log10-transform the cube root of all entries
    mat.trans <- log10(mat^(1/3))
    

    Or with your dataframe example (which is not the same as a matrix):

    df <- read.table(text = 
        "Name      Mass
        1 person_a   234
        2 person_b   190
        3 person_c   203
        4 person_d   176", sep = "");
    
    # log10-transform the cube root
    df$transMass <- log10(df$Mass^(1/3));
    #      Name Mass transMass
    #1 person_a  234 0.7897386
    #2 person_b  190 0.7595845
    #3 person_c  203 0.7691653
    #4 person_d  176 0.7485042