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!

2

2 Answers

# 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 
3

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

df$New<-log10(df$Mass^(1/3)) 
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.