I want to replace the values in one data frame by values in another data frame given that a value exists in the first data frame.
The first data frame consists of values that are either 1 or 0 with NA’s being placeholders.
a <- structure(list(A.1 = c(1L, 0L, NA, 1L, 1L, 0L), A.2 = c(1L, 1L,
1L, NA, 1L, NA), A.3 = c(0L, NA, NA, 0L, NA, NA), A.4 = c(NA,
1L, 1L, NA, 0L, NA), A.5 = c(NA, NA, 1L, NA, NA, NA), A.6 = c(NA,
NA, 1L, NA, NA, 1L)), .Names = c("A.1", "A.2", "A.3", "A.4",
"A.5", "A.6"), class = "data.frame", row.names = c(NA, -6L))
The 2nd dataframe consists of the values that should replace the 1s or 0s with respect to he position of the NAs in the first data frame
r <- structure(list(R.1 = c(45L, 33L, 44L, 24L, 32L, 22L), R.2 = c(33L,
99L, 44L, 25L, 25L, 33L), R.3 = c(22L, 77L, 22L, NA, 26L, NA),
R.4 = c(NA, NA, 32L, NA, NA, NA)), .Names = c("R.1", "R.2",
"R.3", "R.4"), class = "data.frame", row.names = c(NA, -6L))
The goal is this:
d <- structure(list(D.1 = c(45L, 33L, NA, 24L, 32L, 22L), D.2 = c(33L,
99L, 44L, NA, 25L, NA), D.3 = c(22L, NA, NA, 25L, NA, NA), D.4 = c(NA,
77L, 44L, NA, 26L, NA), D.5 = c(NA, NA, 22L, NA, NA, NA), D.6 = c(NA,
NA, 32L, NA, NA, 33L)), .Names = c("D.1", "D.2", "D.3", "D.4",
"D.5", "D.6"), class = "data.frame", row.names = c(NA, -6L))
Any & all suggestions welcome.
Also, notice that
t()returns a matrix instead of data.frame