hputter / mstate

https://hputter.github.io/mstate/
7 stars 5 forks source link

Update msprep.R #26

Closed fumi-github closed 5 months ago

fumi-github commented 1 year ago

The following part within the msprep function sometimes does not work as intended because msres[, 1] is numeric and 1:length(idlevels) is integer. It seems to occur when the vectors are very long.

if (!is.null(idlevels)) 
  msres[, 1] <- factor(msres[, 1], 1:length(idlevels), 
                       labels = idlevels)

To reproduce this issue, download a sample dataset and execute the following code. msrescol1.rds idlevels.rds

msrescol1 = readRDS("msrescol1.rds")
idlevels = readRDS("idlevels.rds")
resultold = factor(msrescol1, 1:length(idlevels), labels = idlevels)
resultnew = factor(as.integer(msrescol1), 1:length(idlevels), labels = idlevels)
setdiff(idlevels, resultold) # not as intended
# [1] "2175662" "3351020" "4526956" "5704306"
setdiff(idlevels, resultnew) # as intended
# character(0)
nickhir commented 5 months ago

I also encountered the error. Happens when length(msres[,1]) > 100000. This fixes the problem

edbonneville commented 5 months ago

@fumi-github thank you picking this up + for the reproducible example (sorry about the delay in replying/hiatus in package maintenance), and thank you @nickhir for also reporting the issue - this PR can be merged!