You did not provide a suitable dataset reprex and therefore I made a small version of your continuous_dataset
with only one column.
And I adapted the function to replace the column (your version adds a category and overwrites that for each column). See if this works for you:
suppressPackageStartupMessages(
suppressWarnings(
{library(dplyr)
library(truncnorm)
}
)
)
set.seed(2022)
continuous_dataset <- data.frame(
col1 = c(1,NA,3,NA,5)
) %>%
print()
#> col1
#> 1 1
#> 2 NA
#> 3 3
#> 4 NA
#> 5 5
ff=function(df1,colnumber,minn,maxx,meann,sdd){
for(i in 1:nrow(df1)){
a <- round(rtruncnorm(1,minn,maxx,meann,sdd))
# df1=df1 %>% mutate(category = if_else((is.na(df1[[i,colnumber]])), a,df1[[i,colnumber]]))
b <- df1[[i,colnumber]]
df1[[i,colnumber]] <- if_else(is.na(b), a, b)
}
return(df1)
}
continuous_dataset2=ff(continuous_dataset,1,120,150,88,54) %>%
print()
#> col1
#> 1 1
#> 2 124
#> 3 3
#> 4 122
#> 5 5
Created on 2022-03-12 by the reprex package (v2.0.1)