آموزش آمار و روش تحقیق
  • صفحه اصلی
  • دوره های آموزشی
    • جامع ترین دوره ی آموزش آمار (توصیفی + استنباطی)
    • دوره ی جامع روش تحقیق کمی، کیفی و آمیخته
    • دوره آمار پرستاری
    • دوره ی جمع بندی و مرور سریع آمار
    • دوره ی جمعبندی و مرور سریع روش تحقیق
  • قفل نرم افزار
  • آموزش های رایگان
  • ورود/عضویت
  • ثبت تیکت
    • تیکت های من

آموزش r برای دانشجویان روانشناسی

نمونه گیری تصادفی در R

Generate a set of Sample data from a Data set in R Programming – sample() Function

sample() function in R Language creates random sample based on the parameters provided in the function call. It takes either a vector or a positive integer as the object in the function parameter.

				
					Syntax:
sample(data, size, replace = FALSE, prob = NULL)

Parameters:
data can be a vector or a dataframe
size represents the size of the sample
replace is used to set the values again repeated if it is set to true
prob: a vector of probability weights for obtaining the elements of the vector being sampled
				
			

Example 1: Generate sample data from the vector

				
					# consider the vector
data=c(23,45,21,34,5,6,7,8,86,45,3)

# get 4 random elements
print(sample(data,4))

# get 1 random element
print(sample(data,1))

# get 6 random elements
print(sample(data,6))

				
			
				
					Output:

[1] 45  7  5 34
[1] 3
[1]  5 23  8 21  6 45

				
			

Example 2:

				
					# Create sample
x <- sample(1:100, 10, replace=TRUE)

# Print
# Output may differ each time the command is executed
print(x)


				
			
				
					#output

[1] 47 52 22 98 75 94 91 94 42 53

				
			

Example 3: Random Sampling of Data Frame Rows Using sample Function

				
					# create dataframe with 2 columns
data=data.frame(col1=c(1:10),col2=c(12:21))

# get the sample of 4 in each column
data[sample(1:nrow(data), size = 4), ]


				
			

#output

نمونه گیری در r

Example 4:Random Sampling of Data Frame Rows Using sample Function

				
					#از داده های mtcars میخواهیم استفاده کنیم
# Create sample
pos <- sample(1:nrow(mtcars), 5, replace = TRUE)

# Print sample observations
# Output may differ each time the command is executed
print(mtcars[pos, ])

#or

mtcars[sample(1:nrow(mtcars), 5, replace = TRUE),]
				
			
				
					#output

                  mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Honda Civic      30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Merc 240D        24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Hornet 4 Drive   21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet 4 Drive.1 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Fiat 128         32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
				
			

Example 5:

				
					> df = data.frame(matrix(rnorm(20), nrow=10))

> df[sample(nrow(df), 3), ]

				
			
				
					
           X1         X2
9  -0.3983045  0.7157506
2  -1.1334614 -0.1973846
10 -0.9060305  2.3234110
				
			

منبع
https://www.geeksforgeeks.org/generate-a-set-of-sample-data-from-a-data-set-in-r-programming-sample-function/

دیدگاهتان را بنویسید لغو پاسخ

برای نوشتن دیدگاه باید وارد بشوید.

دوره های آموزشی ما برای کنکور ارشد و دکتری رشته های روانشناسی، مشاوره، علوم تربیتی، پرستاری, مدیریت آموزشی و علوم شناختی کاربرد دارد.

Instagram Telegram Eaparat Whatsapp Ticket-alt

پرسش و پاسخ و رفع اشکال : از طریق تیکت

دوره های رایگان

دانلود جزوه فرمولات

دوره های آموزشی

دوره جامع آمار

دوره جامع روش تحقیق

دوره جمعبندی آمار

دوره جمعبندی روش تحقیق

دوره آمار پرستاری

سبد خرید