# why value doesn't replace with NA in Tibble column cell

I write this function to generate random numbers in a normal distribution with min, max, mean, sd.
The c argument is column number .
My main problem is that my function seems to work, but the value doesn't replace Tibble
Note = each cell of the column has a specific random number.
Note2= "a" variable is a random number
this is code :

``````ff=function(c,minn,maxx,meann,sdd){
for(i in 1:nrow(continuous_dataset)){
a=round(rtruncnorm(1,minn,maxx,meann,sdd))
continuous_dataset %>% mutate(category = if_else((is.na(continuous_dataset[[i,c]])), a, continuous_dataset[[i,c]]))
}
return(continuous_dataset)
}
``````

this line does a `mutate` on a copy of `continuous_dataset` and will show the result.
However it will not change `continuous_dataset` .
For that use

``````continuous_dataset <- continuous_dataset %>% mutate
``````

it doesn't work, unfortunately

Sorry to hear that.
Can you show us the whole function (after the suggested change)?

In the last line, I call the function

``````ff=function(c,minn,maxx,meann,sdd){
for(i in 1:nrow(continuous_dataset)){
a=round(rtruncnorm(1,minn,maxx,meann,sdd))
continuous_dataset=continuous_dataset %>% mutate(category = if_else((is.na(continuous_dataset[[i,c]])), a, continuous_dataset[[i,c]]))
}
return(continuous_dataset)
}

continuous_dataset=ff(9,120,150,88,54)
``````

Can you show us the first few lines of `continuous_dataset`?
Because you said that the code worked fine and only the tibble was not changed I paid little attention to the other parts of the function
Because your code is not working as you expect, it is worthwhile to be a little more careful.
E.g. your code overwrites your input: that is okay when the code is working fine but until then I would avoid that because it is not very transparent. I would change the code as follows to make debugging somewhat easier:

``````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]]))
}
return(df1)
}

continuous_dataset2=ff(continuous_dataset,9,120,150,88,54)
``````

this is first few line

``````Sys.setlocale(locale = "persian")
##split continuous features
continuous_dataset=dataset[,c(12,20,31,74,85,88,111,112,160,161,
163:169,171,174:196,198:211,238,240,249,254,
255,258,278,292,293,301,302,310,312,314)]
gsub("*", "", continuous_dataset)
continuous_dataset[] <- suppressWarnings(lapply(continuous_dataset, as.numeric))
``````

I run your code, and a stop sign appears, but I don't have any changes in Tibble.
Is there another way to do my function?
class of continuous_dataset is : [1] "tbl_df" "tbl" "data.frame"

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)
``````

It works. Thank you for your follow-up.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.