![]() In the above code samplefrac () function selects random 20 percentage of rows from mtcars dataset. First parameter contains the data frame name, the second parameter tells what percentage of rows to select. ![]() To sample 50 elements from matrix M2, add the following code to the above snippet − M2<-matrix(rnorm(40),ncol=2) If you execute all the above given snippets as a single program, it generates the following output − 1 6 1 3 5 2 2 3 0 3 3 4 1 2 3 2 2 1 5 2įollowing snippet creates a matrix − M2<-matrix(rnorm(40),ncol=2) To sample 20 elements from matrix M1, add the following code to the above snippet − M1<-matrix(rpois(40,2),ncol=2) To take a random sample from a matrix in R, we can simply use sample function and if the sample size is larger than the number of elements in the matrix replace=TRUE argument will be used.įor example, if we have a matrix called M that contains 100 elements and we want to sample 200 elements from M then we can use the below given command − sample(M,200,replace=TRUE) Example 1įollowing snippet creates a matrix − M1<-matrix(rpois(40,2),ncol=2)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |