R 101: The Subset Function
[This article was first published on Abraham Mathew » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The subset function is available in base R and can be used to return subsets of a vector, martix, or data frame which meet a particular condition. In my three years of using R, I have repeatedly used the subset() function and believe that it is the most useful tool for selecting elements of a data structure. I assume that many of you are familiar with this function, so I will simply conclude this post by providing some brief examples of the subset function.
numvec = c(2,5,8,9,0,6,7,8,4,5,7,11) charvec = c("David","James","Sara","Tim","Pierre", "Janice","Sara","Priya","Keith","Mark", "Apple","Sara") gender = c("M","M","F","M","M","M","F","F","F","M","M","F") state = c("CO","KS","CA","IA","MO","FL","CA","CO","FL","CA","WY","AZ") subset(numvec, numvec > 7) subset(numvec, numvec < 9 & numvec > 4) subset(numvec, numvec < 3 |numvec > 9) df = data.frame(var1=c(numvec), var2=c(charvec), gender=c(gender), state=c(state)) subset(df, var1 < 5) subset(df, var2 == "Sara") subset(df, var1==5, select=c(var2, state)) subset(df, var2 != "Sara" & gender == "F" & var1 > 5)
To leave a comment for the author, please follow the link and comment on their blog: Abraham Mathew » R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.