0

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.

KNN
  • 459
  • 4
  • 19
  • 1
    Dataframe column names are NOT supoposed to start with digits, so you should probably be quoting or backticking the "2108" column names when using $ access. You also have two different spellings "Mono" and "Mono." Perhaps you should be showing use instead the values of `Mono$cov` – IRTFM Feb 25 '19 at 21:28
  • Thanks. The Mono. was a typo here. I changed the col name to yr2018 and still same issues. – KNN Feb 25 '19 at 23:22
  • Well we still don't have solid, useful information about `Mono`. So you are asking for speculation rather than supplying necessary basis for coding. – IRTFM Feb 25 '19 at 23:25
  • I added the data for the Mono column I want to convert given the lookup table. Thank you. – KNN Feb 26 '19 at 02:24
  • All the `lookup` columns are numerics. You should make `Mono$cov` numeric as well. You do try to do this, you have `as.numeric(as.character())` as one should. But then you use `gsub` on it to remove spaces. Numerics *don't have spaces*, so this is pointless, and string-modifying functions like `gsub` return `character` vectors, so all your `gsub` does is undo your `as.numeric`. Either (a) delete `gsub` entirely, or (b) if you think you need to remove spaces, do it *before* (inside) the `as.numeric`, not after (outside). – Gregor Thomas Feb 26 '19 at 03:38
  • That said, testing non-integer numerics for equality is fraught as well due to [floating point precision issues](https://stackoverflow.com/q/9508518/903061). Looks like the values you are matching on are all integers, so you should be fine. – Gregor Thomas Feb 26 '19 at 03:41
  • Ahha. Very helpful. This got me closer but the lookup table substitutions are still off I used this code: `Mono$cov <- as.numeric(as.character(gsub(' ', '', Mono$cov))) Mono$covTrans <- lookup[as.numeric(Mono$cov), 'mid']`. Results were: `$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. – KNN Feb 26 '19 at 04:20

0 Answers0