Generating Data by using rnorm function with a condition

#1

Hi, I'm a new user of R. I'm trying to generate data by using rnorm function with "sum of data must be 0" condition. Can u help me?

0 Likes

#2

Welcome to R !

Have you tried anything ? Have you any error or question ?

For now it looks like you are asking to solve an exercice for you.

If you need ressource for beginners, we'll be happy to provide some.

(Just in case, FAQ: Homework Policy)

0 Likes

#3

As Christophe says, you need to be more specific, for example this would satisfy your request but I don't think is what you are looking for.

set.seed(123)
x <- rnorm(10)
x <- c(x, -x)
x
#>  [1] -0.56047565 -0.23017749  1.55870831  0.07050839  0.12928774
#>  [6]  1.71506499  0.46091621 -1.26506123 -0.68685285 -0.44566197
#> [11]  0.56047565  0.23017749 -1.55870831 -0.07050839 -0.12928774
#> [16] -1.71506499 -0.46091621  1.26506123  0.68685285  0.44566197
sum(x)
#> [1] 0

Created on 2019-02-11 by the reprex package (v0.2.1)

0 Likes

#5

Hi,
I had a problem with my PC but I come back again. Actually, this is not what I want. I want to generate data with rnorm for example rnorm(5)
[1] 1.6264217 -0.1686985 -0.2744524 0.8593511 1.4194899
And then I want to generate 5 number for each data that sum of these is equal to negative of data cause of their sum must be 0.
Ex.
for the first data (1.6264217): 0.913500, -0.598748, -0.670174, -1.686074, 0.415074 (their sum= -1.6264217)

an then I'll do this for each data.

Can this be done with R?

0 Likes

#6

Are you looking for something like this?

x <- rnorm(n = 10)
y <- sapply(X = x,
            FUN = function(t)
            {
              temp <- rnorm(n = 5)
              std_temp <- scale(x = temp,
                                scale = FALSE)
              return(std_temp - (t / 5))
            })

y
#>             [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
#> [1,] -0.78447371  0.2982795  0.3188463  1.2688983 -0.3447385  0.5116717
#> [2,]  0.36055242 -2.3718419 -0.2294217  0.6236303  0.5936671  0.6530591
#> [3,] -0.37618191  0.8355898  0.2671856 -0.7449741 -0.2968168 -2.3798615
#> [4,]  0.63759853  2.4088066  0.3548191 -0.4620816 -0.7909975 -0.2509536
#> [5,]  0.05087553 -0.2942573 -0.2215604 -0.5095958  0.3520312  0.0117008
#>             [,7]        [,8]        [,9]       [,10]
#> [1,] -0.01016194  0.57710747 -0.39538632  0.26427684
#> [2,]  1.40631458  1.05989171 -0.63790931  1.62911131
#> [3,]  1.18464826 -0.07724773 -0.25789336 -1.60000096
#> [4,] -0.48098178 -1.35590209  0.05574678 -1.63405475
#> [5,] -0.84194538 -0.56453635  0.05751673  0.09449964

data.frame("Col_Sum_y" = colSums(x = y),
           "x" = x)
#>     Col_Sum_y          x
#> 1  -0.1116291  0.1116291
#> 2   0.8765767 -0.8765767
#> 3   0.4898690 -0.4898690
#> 4   0.1758771 -0.1758771
#> 5  -0.4868545  0.4868545
#> 6  -1.4543835  1.4543835
#> 7   1.2578737 -1.2578737
#> 8  -0.3606870  0.3606870
#> 9  -1.1779255  1.1779255
#> 10 -1.2461679  1.2461679
1 Like

#7

Yeah, that's exactly what I'm looking for! Thank you so much :slight_smile:

0 Likes

#8

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

0 Likes

closed #9

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.

0 Likes