Aisling Murray
Aisling Murray

Reputation: 49

adding new column to dataframe using formula

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

Upvotes: 0

Views: 715

Answers (2)

Uwe
Uwe

Reputation: 42592

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"
))

Upvotes: 0

Peter K
Peter K

Reputation: 726

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)])

Upvotes: 1

Related Questions