I have two data.frames A
(dat1
) and B
(dat2
).
Is it possible to find small differences (up to tol
) between one or more numeric columns (cols
) across A
and B
and the replace those in A
with the corresponding ones in B
For example, if you look at the numeric columns across A
and B
, you'll see THIS
in A
for column upper.CL
is 1.770 but the same in B
is 1.771 i.e., they are different by tol = .001
. In this case, all we need is to replace 1.770 in A
with 1.771 from B
so that all numeric columns in A
and B
are the same.
Is it possible to write an R
function to find & use all numeric columns that differ by tol
and replace the values as described above?
foo <- function(dat1, dat2, cols = NULL, tol){
# Solution
}
# EXAMPLE OF USE:
#### foo(dat1 = A, dat2 = B, cols = upper.CL, tol = .002)
# OR
#### foo(dat1 = A, dat2 = B, tol = .002)
A = read.table(h=TRUE, text="
task_dif time emmean SE lower.CL upper.CL
1 complex 1 1.733 0.023 1.686 1.779
2 simple 1 1.734 0.018 1.697 1.770# <- THIS
3 complex 2 1.702 0.025 1.652 1.751
4 simple 2 1.714 0.017 1.680 1.747
5 complex 3 1.757 0.019 1.720 1.794
6 simple 3 1.740 0.027 1.687 1.794
7 complex 4 1.773 0.019 1.735 1.810
8 simple 4 1.764 0.025 1.713 1.814")
B = read.table(h=TRUE, text="
order time emmean SE lower.CL upper.CL
1 c2s 1 1.733 0.023 1.686 1.779
2 s2c 1 1.734 0.018 1.697 1.771# <- THIS
3 c2s 2 1.714 0.017 1.680 1.747
4 s2c 2 1.702 0.025 1.652 1.751
5 c2s 3 1.757 0.019 1.720 1.794
6 s2c 3 1.740 0.027 1.687 1.794
7 c2s 4 1.764 0.025 1.713 1.814
8 s2c 4 1.773 0.019 1.735 1.810")
Desired output:
A = read.table(h=TRUE, text="
task_dif time emmean SE lower.CL upper.CL
1 complex 1 1.733 0.023 1.686 1.779
2 simple 1 1.734 0.018 1.697 1.771# <- Replaced using corresponding value in `B`
3 complex 2 1.702 0.025 1.652 1.751
4 simple 2 1.714 0.017 1.680 1.747
5 complex 3 1.757 0.019 1.720 1.794
6 simple 3 1.740 0.027 1.687 1.794
7 complex 4 1.773 0.019 1.735 1.810
8 simple 4 1.764 0.025 1.713 1.814")
B = read.table(h=TRUE, text="
order time emmean SE lower.CL upper.CL
1 c2s 1 1.733 0.023 1.686 1.779
2 s2c 1 1.734 0.018 1.697 1.771# <- THIS
3 c2s 2 1.714 0.017 1.680 1.747
4 s2c 2 1.702 0.025 1.652 1.751
5 c2s 3 1.757 0.019 1.720 1.794
6 s2c 3 1.740 0.027 1.687 1.794
7 c2s 4 1.764 0.025 1.713 1.814
8 s2c 4 1.773 0.019 1.735 1.810")