0

I have a dataframe and the head() looks like this:

                  CEMETERY    SEX CONTEXT RaHD L RaHD R DIRECTIONAL ASYMMETRY
1 Medieval-St. Mary Graces FEMALE    7172   21.2   21.6                    NA
2 Medieval-St. Mary Graces   MALE    6225   23.9   25.2                    NA
3 Medieval-St. Mary Graces   MALE    9987   23.9   23.5                    NA
4 Medieval-St. Mary Graces   MALE   11475   22.4   22.3                    NA
5 Medieval-St. Mary Graces   MALE   12356   25.8   25.4                    NA
6 Medieval-St. Mary Graces   MALE   12525   22.4   22.3                    NA

(RaHD L and RaHD R are bone measurements). I have just created the 'DIRECTIONAL ASYMMETRY' column by doing:

MRaHDTABLE["DIRECTIONAL ASYMMETRY"]=NA

and I now need to input data into that column. The formula for directional asymmetry is '%DA = (right - left) / (average of left and right) x 100' so would be (RaHD R - RaHD L) / (average of RaHD R and RaHD L) x 100. I'm not sure how to input this into my table as I tried:

MRaHDTABLE$'DIRECTIONAL ASYMMETRY'=(MRaHDTABLE$`RaHD R`-
MRaHDTABLE$`RaHDL`)/mean(MRaHDTABLE$`RaHD L`,MRaHDTABLE$`RaHD R`)*100

but got the error:

Error in mean.default(MRaHDTABLE$`RaHD L`, MRaHDTABLE$`RaHD R`) : 
  'trim' must be numeric of length one
8
  • 1
    I'd recommend using valid row names in the future for ease of referencing columns, and clarity. RaHD.L and RaHD_L are both good alternatives. Commented Dec 24, 2017 at 4:39
  • 1
    Is the average for a particular row or is it for the whole data? for example the first value you need. is it (21.6 -21.2)/((21.6-21.2)/2)*100 or do you take the average of the whole column? Commented Dec 24, 2017 at 4:39
  • I need the average for each row (e.g. for the first row - (21.6+21.2)/2=21.4) - sorry didn't see the last part of your message - it would be that first equation you put Commented Dec 24, 2017 at 4:43
  • Define a little function DA <- function(x, y) {(x-y)/mean(x,y)*100}, and then mapply(DA, x= MRaHDTABLE$'RaHD L', y= MRaHDTABLE$'RaHD R') Commented Dec 24, 2017 at 4:47
  • @AislingMurray No prob, moving my comment to an actual answer Commented Dec 24, 2017 at 4:53

2 Answers 2

1

You are using the mean function incorrectly in your formula. If you look at the documentation (?mean), the function takes three arguments: a numeric vector (x), the fraction of values to be trimmed (trim), and how to treat missing values (na.rm). Therefore, in your specification mean(MRaHDTABLE$`RaHD L`,MRaHDTABLE$`RaHD R`), the first term is interpreted as the input vector (x),and the second term is interpreted as the the trim parameter.

Try replace

mean(MRaHDTABLE$`RaHD L`,MRaHDTABLE$`RaHD R`)

With

rowMeans(name_of_df[ , c(4,5)])
Sign up to request clarification or add additional context in comments.

Comments

0

The OP has asked to implement the formula

(RaHD R - RaHD L) / (average of RaHD R and RaHD L) x 100

The answers posted so far are trying to make the mean() function work row-wise just to compute the average of two numbers which simply is

average of RaHD R and RaHD L = (RaHD R + RaHD L) / 2

So, the answer could be as simple as:

MRaHDTABLE["DIRECTIONAL.ASYMMETRY"] <- 
  with(MRaHDTABLE, 200 * (RaHD.R - RaHD.L) / (RaHD.R + RaHD.L))
MRaHDTABLE
  i           X2    CEMETERY    SEX CONTEXT RaHD.L RaHD.R DIRECTIONAL.ASYMMETRY
1 1 Medieval-St. Mary Graces FEMALE    7172   21.2   21.6             1.8691589
2 2 Medieval-St. Mary Graces   MALE    6225   23.9   25.2             5.2953157
3 3 Medieval-St. Mary Graces   MALE    9987   23.9   23.5            -1.6877637
4 4 Medieval-St. Mary Graces   MALE   11475   22.4   22.3            -0.4474273
5 5 Medieval-St. Mary Graces   MALE   12356   25.8   25.4            -1.5625000
6 6 Medieval-St. Mary Graces   MALE   12525   22.4   22.3            -0.4474273

Note The data look differently to OP's posted data. This is due to OP's failure to provide sample data in a reproducible way, i.e., by posting the result of dput(MRaHDTABLE). So, I tried to reproduce the data with a less effort as possible.

The with() function is used to save typing.

Data

MRaHDTABLE <- data.frame(readr::read_table(
 "  i                 CEMETERY    SEX CONTEXT RaHD.L RaHD.R DIRECTIONAL.ASYMMETRY
  1 Medieval-St. Mary Graces FEMALE    7172   21.2   21.6                    NA
  2 Medieval-St. Mary Graces   MALE    6225   23.9   25.2                    NA
  3 Medieval-St. Mary Graces   MALE    9987   23.9   23.5                    NA
  4 Medieval-St. Mary Graces   MALE   11475   22.4   22.3                    NA
  5 Medieval-St. Mary Graces   MALE   12356   25.8   25.4                    NA
  6 Medieval-St. Mary Graces   MALE   12525   22.4   22.3                    NA"
))

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.