I think I have hidden factor issues that prevents successful replacement of values from a lookup table. I converted the data frame column Mono$cov from a factor but when I attempt lookup value replacements Mono$cov either revert back to factors or fails. First, I removed spaces and converted:
> class(Mono$cov)
[1] "factor"
Mono$cov <- gsub(' ', '', as.numeric(as.character(Mono$cov)))
> class(Mono$cov)
[1] "character"
Mono$cov
[1] "3" "7" "8" "12" "5" "5"
See below for full dput of Mono$cov. Then I'm trying to replace values in Mono$cov by matching to lookup$yr2018 and replacing with lookup$mid.
If I run the following it errors that NAs are not allowed in subscripted assignments:
Mono$cov[match(lookup$yr2018, Mono$cov)] <- lookup$mid
And only some of the values get replaced and incorrectly:
> Mono$cov
[1] 3.0 45.0 75.0 10.0 25.0 5.0
If I use this code I get mainly NAs:
Mono$covTrans <- lookup[as.character(Mono$cov), 'mid']
> Mono$covTrans
[1] NA 97.0 99.5 NA NA NA NA NA
dput(lookup)
structure(list(low = c(0L, 0L, 1L, 5L, 10L, 20L, 30L, 40L, 50L,
60L, 70L, 80L, 90L, 95L, 99L), high = c(0, 1, 5, 10, 20, 30,
40, 50, 60, 70, 80, 90, 95, 99, 100), yr2018 = c(0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), mid = c(0, 0.5, 3,
7.5, 15, 25, 35, 45, 55, 65, 75, 85, 92.5, 97, 99.5)), .Names = c("low",
"high", "2018", "mid"), row.names = c("0", "1", "2", "3-",
"3+", "4-", "4*", "4+", "5-", "5*", "5+", "6-", "6+", "7", "8"
), class = "data.frame")
EDIT: added Mono$cov data here:
> head(dput(Mono$cov))
c("3", "7", "8", "12", "5", "5", "11", "12", "5", "9", "10",
"0", "11", "13", "10", "11", "11", "10", "9", "1", "12", "4",
"12", "10", "9", "4", "11", "4", "5", "3", "8", "5", "5", "13",
"5", "9", "3", "12", "9", "9", "12", "10", "4", "9", "13", "9",
"9", "6", "6", "4", "13", "9", "9", "7", "8", "10", "4", "10",
"4", "7", "11", "13", "9", "10", "7", "2", "6", "0", "9", "0",
"12", "10", "8", "12", "10", "11", "9", "3", "13", "10", "6",
"10", "4", "1", "5", "0", "13", "3", "12", "8", "3", "9", "0",
"0", "10", "4", "9", "12", "5", "2", "0", "4", "10", "4", "5",
"9", "2", "8", "13", "9", "6", "9", "11", "5", "9", "5", "13",
"8", "7", "3", "9", "12", "12", "4", "3", "3", "9", "11", "5",
"0", "4", "13", "7", "11", "5", "7", "11", "10", "13", "6", "4",
"0", "12", "11", "11", "9", "7", "13", "6", "3", "10", "7", "9",
"11", "0", "9", "4", "7", "13", "12", "13", "10", "12", "11",
"9", "0", "7", "9", "7", "11", "11", "2", "8", "4", "8", "4",
"9", "9", "0", "11", "11", "9", "9", "7", "7", "5", "4", "0",
"10", "8", "3", "10", "3", "6", "11", "5", "0", "4", "9", "9",
"8", "11", "9", "13", "9", "0", "7", "4", "13", "4", "3", "6",
"2", "4", "9", "8", "4", "4", "7", "10", "7", "10", "5", "8",
"4", "12", "12", "7", "10", "8", "2", "0", "3", "11", "11", "12",
"6", "10", "4", "10", "8", "12", "12", "4", "5", "0", "8", "7",
"13", "8", "3", "7", "2", "5")
[1] "3" "7" "8" "12" "5" "5"
UPDATE:
Modifying my code as suggested still doesn't work correctly.
Mono$cov <- as.numeric(as.character(gsub(' ', '', Mono$cov))) Mono$covTrans <- lookup[as.numeric(Mono$cov), 'mid']
.
Results are:
$covTrans [1] 3.0 35.0 45.0 85.0 15.0
when the first value should convert 3 to 7.5, given the lookup table and so on.