## calculate ratio of two factors for each visit using dplyr

Question

I'd like to use `dplyr`

to calculate a vector of ratios of two factors for each visit of a subject. The mock data can be created below:

```
subj = c(rep("A", 10), rep("B", 4), rep("C", 6))
vist = c(rep(c("C0", "C1", "C2", "C3", "C4"), each=2),
rep(c("C0", "C1"), each=2),
rep(c("C0", "C1", "C2"), each=2))
factor = c(rep(c("L", "N"), 5), rep(c("L", "N"), 2), rep(c("L", "N"), 3))
set.seed(111)
aval = round(rnorm(n = 20, 0, 1), 2)
dat = data.frame(subj, vist, factor, aval, stringsAsFactors = FALSE)
dat
```

Which looks like:

```
subj vist factor aval
1 A C0 L 0.24
2 A C0 N -0.33
3 A C1 L -0.31
4 A C1 N -2.30
5 A C2 L -0.17
6 A C2 N 0.14
7 A C3 L -1.50
8 A C3 N -1.01
9 A C4 L -0.95
10 A C4 N -0.49
11 B C0 L -0.17
12 B C0 N -0.41
13 B C1 L 1.85
14 B C1 N 0.39
15 C C0 L 0.80
16 C C0 N -1.57
17 C C1 L -0.09
18 C C1 N -0.36
19 C C2 L -1.19
20 C C2 N 0.36
```

What is needed is the ratio of value (`aval`

) for the factors (`factor`

) "N" over "L", for each subject (`subj`

) for each visit (`vist`

). For example, the first ratio value would be `-1.375`

, coming from `-0.33/0.24`

. Thanks!

Show source

## Answers to calculate ratio of two factors for each visit using dplyr ( 4 )

You can reshape the data with

`spread`

from the tidyr package, then it's easy to calculate a new column:If there is only one

`N`

and`L`

per group, you can do:In base R, you can use

`aggregate`

to construct the summary of the ratios or`ave`

to fill in these ratios into your original data.frame. This assumes that the data.frame is regular and ordered correctly.or

will add it as a variable.

If these are in the same order and have exactly one pair per each 'subj', 'vist'