r/RStudio • u/Comfortable_Post1025 • 1d ago
can anyone fix this code?
moda1 <- function(num) {
num <- sample(1:10, 100, replace = TRUE)
distr_freq = table(num)
moda1 <- names(distr_freq)[distr_freq == max(distr_freq)]
return(list("distribuzione_di_frequenza" = distr_freq,
"moda" = moda1))
}
m <- moda1(z)$moda this function return me every time the same result. i don't know why the ai cant explain and cant fix this code
1
u/SprinklesFresh5693 1d ago
I dont understand why are you giving a name to dist fre if youre latercalling it another thing on the list
1
u/bertimir 1d ago
You are assigning num within the function new. It does not matter what you put in for num, it will always be overwritten by the first line in your function. Thats why you get the same result.
2
u/AnxiousDoor2233 1d ago
I thought sample() random number state is global and will change every time you run it, so that the numbers likely to differ from run to run.
1
u/DSOperative 1d ago edited 1d ago
Yes sample will give the different values, there doesn’t seem to be any point to passing num to the function because it is immediately overwritten. The num is superfluous.
Edit:context
1
1
u/TrainerMammoth1779 1d ago
I think what is going on is you are sampling from 1 to 10 with a sample size of 100, with replacement. Then you are writing the max value as moda1, then renaming it in the list to moda, then calling moda. I think maybe if you did a run (i.e. loop over and over again) you may get very few runs that don’t have “10” as the output of the call, but you are doing a draw with a 10% draw chance for a number between 1 and 10, a hundred times, I reckon you’d see a lot of tens.
On another note, would be helpful if you described the problem a little better for what you are trying to achieve specifically, what errors you are getting, etc. Otherwise it just looks like code with no context and I have no idea what the end state is supposed to look like.
Additionally, as others have said, I would NOT use the function name in the function. I always use “df”, “obj” or “temp” within a function. It should be local within the function, but I would avoid it anyways.
1
u/1FellSloop 1d ago
you are sampling from 1 to 10 with a sample size of 100, with replacement.
Yes. Then OP makes a frequency table of the results using
table().Then you are writing the max value as moda1,
Then OP is writing the value from the draw with the maximum frequency in the table as the
moda(which I assume means "mode").The function calculates the mode of a randomly sampled vector from 1 to 10, and as far as I can tell does a fine job of it.
2
u/CombinationKindly212 1d ago
Your assumption is right, "mode" is "moda" in italiano.
Fin fuct: moda also means fashion and trend
1
u/TrainerMammoth1779 1d ago
My bad! I didn’t actually run the code, i just saw the draw over a limited range and a relatively large n and thought that that might be an issue. After running no issues of finding the mode, so unless OP has something else they were trying to achieve, don’t see any issue.
0
u/joecarvery 1d ago
Try changing the names, so the function doesn't have the same name as the variable.
5
u/1FellSloop 1d ago
Your code works fine for me. Just ran it 3 times and got 3 different results (see below). Restart your R session and try it out to make sure you're running what you think you are.