M3003 M3003 - 3 months ago 23
R Question

Error in using lapply?

I want to divide each column of my dataframe "data" by each column of another dataframe called "benchmark". However, I get different results using lapply and dividing it manually. Where is the error in my code?

The code I use is:

div <- data.frame(lapply(data, function(x) x[col(benchmark)]/benchmark))


For the first two columns, this gives me the following result...

div.A.B1 div.A.B2
1 0.7200000 0.8000000
2 0.7422680 0.8163265
3 0.7346939 0.8080808
4 0.7422680 0.8333333
5 0.7578947 0.8510638
6 0.7741935 0.8695652
7 0.7826087 0.8510638
8 0.7826087 0.8602151
9 0.7912088 0.8791209
10 0.8181818 0.8791209


... while dividing the first column of "data" by the first two columns of "benchmark" manually gives me:

A.B1 A.B2
1 0.7200000 0.7200000
2 0.8247423 0.8163265
3 0.7653061 0.7575758
4 0.7525773 0.7604167
5 0.9473684 0.9574468
6 0.8709677 0.8804348
7 0.8804348 0.8617021
8 0.9347826 0.9247312
9 1.0989011 1.0989011
10 0.9090909 0.8791209


Some sample data for "data":

A
1 72
2 80
3 75
4 73
5 90
6 81
7 81
8 86
9 100
10 80


and for "benchmark":

B1 B2
1 100 100
2 97 98
3 98 99
4 97 96
5 95 94
6 93 92
7 92 94
8 92 93
9 91 91
10 88 91

Answer

I think you might want to try purrr for this one, it has some functions which let you map across multiple lists which is helpful for this case. In this case you could use something like map2_df(data, benchmark, ~.x / .y)