1

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!

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Simos
  • 33
  • 1
  • 1
  • 6
  • That's not a matrix you're giving as example. Please provide a [MWE](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with expected output. Assuming you have an actual `matrix` (and not a `dataframe`), see my sample solution below. – Maurits Evers Nov 28 '17 at 22:24
  • Yes, I realize that. Thanks for the link and your answer! – Simos Nov 28 '17 at 22:33

2 Answers2

3
# 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
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
1

Assuming you have dataframe df and variable named Mass, You can use this:

df$New<-log10(df$Mass^(1/3))
JeanVuda
  • 1,738
  • 14
  • 29