How can I subset a data set? | R FAQ - UCLA
文章推薦指數: 80 %
It is possible to subset both rows and columns using the subset function. The select argument lets you subset variables (columns). The data frame x.sub2 ... SkiptoprimarynavigationSkiptomaincontentSkiptoprimarysidebarTheRprogram(asatextfile)forallthecode onthispage. Subsettingisaveryimportantcomponentofdatamanagementandthere areseveralwaysthatonecansubsetdatainR.Thispageaimstogiveafairly exhaustivelistofthewaysinwhichitispossibletosubsetadatasetinR. Firstwewillcreatethedataframethatwillbeusedinallthe examples.Wewillcallthisdataframex.dfanditwillbecomposedof5variables (V1–V5)where thevaluescomefromanormaldistributionwithamean0andstandarddeviationof1;aswell as,onevariable(y)containingintegersfrom1to5. set.seed(1234) x2) x.sub V1 V2 V3 V4 V5y 4-1.3456980.1099621710.889714510.5093141-0.023655723 5 1.4291250.5228073000.488990490.5594521 0.984861704 6 1.5060560.0016135550.088804581.4595894 0.064051405 Subsettingrowsusingmultipleconditionalstatements Thereisnolimittohowmanylogicalstatementsmaybecombinedtoachievethesubsetting thatisdesired.Thedataframex.sub1containsonlytheobservationsforwhichthe valuesofthevariableyisgreaterthan2andforwhichthevariableV1isgreaterthan0.6. x.sub12&V1>0.6) x.sub1 V1 V2 V3 V4 V5y 51.4291250.5228073000.488990490.55945210.98486174 61.5060560.0016135550.088804581.45958940.06405145 Subsettingbothrowsandcolumns Itispossibletosubsetbothrowsandcolumnsusingthesubsetfunction.The selectargumentletsyousubsetvariables(columns).Thedataframex.sub2 containsonlythevariablesV1andV4andthenonlytheobservationsofthesetwovariables wherethevaluesofvariableyaregreaterthan2andthevaluesofvariableV2aregreaterthan 0.4. x.sub22&V2>0.4,select=c(V1,V4)) x.sub2 V1 V4 51.4291250.5594521 Inthedataframex.sub3containsonlytheobservationsinvariablesV2-V5 forwhichthevaluesinvariableyaregreaterthan3. x.sub33,select=V2:V5) x.sub3 V2 V3 V4 V5 50.5228073000.488990490.55945210.9848617 60.0016135550.088804581.45958940.0640514 Subsettingrowsusingindices Anothermethodforsubsettingdatasetsisbyusingthebracketnotationwhichdesignates theindicesofthedataset.Thefirstindexisfortherowsandthesecondforthecolumns. Thex.sub4dataframecontainsonlytheobservationsforwhichthevaluesofvariableyare equalto1.Notethatleavingtheindexforthecolumnsblankindicatesthatwewantx.sub4to containallthevariables(columns)oftheoriginaldataframe. x.sub4
延伸文章資訊
- 1How can I subset a data set? | R FAQ - UCLA
It is possible to subset both rows and columns using the subset function. The select argument let...
- 2subset function - RDocumentation
Return subsets of vectors, matrices or data frames which meet conditions. ... in recent versions ...
- 3R 语言中subset()函数使用- 何帅 - 博客园
R语言中的subset()应用也可以使用select参数选择指定的列以上是subset函数在结合data.frame进行条件筛选时的常见用法subset函数还可以用在向量上, ...
- 4Subsetting Data - Quick-R
R has powerful indexing features for accessing object elements. These features can be used to sel...
- 5R語言subset和merge函式的使用- IT閱讀
R語言subset和merge函式的使用 · > selectresult=subset(df1,name=="aa",select=c(age,sex)); > · > selectresul...