What is the difference between subset() and sample() function in R?
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 2 )
Sample () function can be used to select a random sample of size ānā from a huge dataset.
Subset () function is used to select variables and observations from a given dataset.
Eg: (sample)
U
[1] a b c d e f g h i j
sample(U,6)
[1] a c b f e i
Eg: (subset)
person
name age
1 Karan 23
2 Akash 27
3 Shweta 19
4 Aman 22
people[[“age”]]
[1] 23 27 19 22
Subset Function in R, returns subset of dataframe, vectors or matrices which meet the specified conditions.
Syntax:
subset(x, condition,select)
x ā can be a matrix ,data frame or vector
condition- condition to be satisfied
select ā columns to be selected
Example:
newdata=30, select=c(mpg,cyl,gear))
Above code selects cars, mpg, cyl, gear from mtcars table where mpg >=30 so the output will be
mpg cyl gear
fiat 30.4 4 5
civic 32.6 4 5
corolla 33.5 4 5
Lotus 37.9 4 5
Sample() function in R, generates a sample of the specified size from the data set or elements, either with or without replacement.
sample(x, size, replace = FALSE, prob = NULL)
x- Data Set or a vector of one or more elements from which sample is to be chosen
size- size of a sample
replace- Should sampling be with replacement?
prob- probability weights for obtaining the elements of the vector being sampled
sample(1:20, 10, replace=TRUE)
When we run the above code the output will be
6 8 12 19 5 18 19 14 13 2