What does the following line in R do? [closed]

-3

I'm doubtful about the following line:

 amostra = sample(2,40,replace = T,prob=c(0.7,0.3))?

In particular the argument 2 , argument 40 and replace .

    
asked by anonymous 09.05.2018 / 00:57

1 answer

5

The syntax of the way it is is a bit strange indeed.

As described in the documentation, the first argument can be a vector, from which you want to take a sample or an integer. If it is an integer, it will sample from 1 to that number. In this case, since it is an integer (2), the function will sample the c(1,2) .

40 , the second argument ( n ), is the sample size you want to remove. That is, this call of the function sample will return an array with 40 elements.

replace is an argument whether the sample is to be made with or without replacement. In case, as you are going to make a size 40 sample of a vector of size 2, it is mandatory to replace ( replace = TRUE ).

Finally, the prob argument allows you to specify probabilities to sort out each of the vector elements you are sampling. In this case, c(0.7, 0.3) indicates that in calling this function you will 1 with 70% probability and 2 with 30%.

To summarize: This line returns a random sample with size 40 replacement of the elements of the c(1,2) vector, with 1 being 70% and 2, 30% probability.

In% with%, documentation is generally fairly good. It pays to read the function help. Within the R itself, if you enter R , a window will appear with the documentation.

    
09.05.2018 / 04:50