banner



How To Filter Dataframe In R

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

How to filter data frame by categorical variable in R?


To filter information frame by categorical variable in R, nosotros can follow the below steps −

  • Use inbuilt data sets or create a new data set up and wait at pinnacle few rows in the data set up.

  • Then, expect at the bottom few rows in the data prepare.

  • Check the information structure.

  • Filter the information by chiselled column using split function.

Use inbuilt information set

Permit's consider CO2 data ready in base R −

 Live Demo

data(CO2) head(CO2,ten)

On executing, the above script generates the beneath output(this output volition vary on your organization due to randomization) −

Grouped Data: uptake ~ conc | Plant   Institute Type Treatment conc uptake i Qn1 Quebec nonchilled 95 16.0 2 Qn1 Quebec nonchilled 175 30.four iii Qn1 Quebec nonchilled 250 34.8 4 Qn1 Quebec nonchilled 350 37.ii 5 Qn1 Quebec nonchilled 500 35.3 6 Qn1 Quebec nonchilled 675 39.two 7 Qn1 Quebec nonchilled g 39.7 eight Qn2 Quebec nonchilled 95 thirteen.half-dozen 9 Qn2 Quebec nonchilled 175 27.3 ten Qn2 Quebec nonchilled 250 37.i

Await at few bottom rows

Use tail function to look at some bottom rows in CO2 data −

 Live Demo

data(CO2) tail(CO2,ten)

Output

Grouped Data: uptake ~ conc | Constitute     Plant  Type      Treatment conc uptake 75  Mc2 Mississippi chilled    500   12.5 76  Mc2 Mississippi chilled    675   xiii.vii 77  Mc2 Mississippi chilled    1000  14.4 78  Mc3 Mississippi chilled    95    ten.6 79  Mc3 Mississippi chilled    175   xviii.0 fourscore  Mc3 Mississippi chilled    250   17.nine 81  Mc3 Mississippi chilled    350   17.9 82  Mc3 Mississippi chilled    500   17.9 83  Mc3 Mississippi chilled    675   18.9 84  Mc3 Mississippi chilled    grand  19.9

Check the data construction

Use str function to check the data construction of data in CO2 −

Example

 Live Demo

data(CO2) str(CO2)

Output

Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame': 84 obs. of 5 variables: $ Plant    : Ord.cistron westward/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 1 1 i one one i 2 2 2 ... $ Type     : Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 ane 1 1 one 1 ane ane ... $ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 i one 1 1 1 1 one 1 i ... $ conc     : num 95 175 250 350 500 675 chiliad 95 175 250 ... $ uptake   : num 16 xxx.4 34.8 37.2 35.3 39.2 39.7 13.half dozen 27.3 37.1 ... - attr(*, "formula")=Form 'formula' language uptake ~ conc | Establish .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> - attr(*, "outer")=Class 'formula' linguistic communication ~Treatment * Type .. ..- attr(*, ".Environs")=<environment: R_EmptyEnv> - attr(*, "labels")=List of ii ..$ ten: chr "Ambient carbon dioxide concentration" ..$ y: chr "CO2 uptake rate" - attr(*, "units")=List of 2 ..$ x: chr "(uL/L)" ..$ y: chr "(umol/m^two due south)"

Filter data frame past categorical column

Using split up office to filter the data frame CO2 based on Type column −

 Live Demo

data(CO2) split(CO2,CO2$Blazon)

Output

$Quebec Grouped Data: uptake ~ conc | Institute     Plant Type Handling conc uptake ane  Qn1 Quebec nonchilled 95    16.0 2  Qn1 Quebec nonchilled 175   thirty.iv 3  Qn1 Quebec nonchilled 250   34.8 4  Qn1 Quebec nonchilled 350   37.2 five  Qn1 Quebec nonchilled 500   35.iii 6  Qn1 Quebec nonchilled 675   39.2 7  Qn1 Quebec nonchilled 1000  39.7 8  Qn2 Quebec nonchilled 95    13.vi ix  Qn2 Quebec nonchilled 175   27.3 x Qn2 Quebec nonchilled 250   37.1 11 Qn2 Quebec nonchilled 350   41.8 12 Qn2 Quebec nonchilled 500   twoscore.6 thirteen Qn2 Quebec nonchilled 675   41.4 14 Qn2 Quebec nonchilled m  44.iii xv Qn3 Quebec nonchilled 95    16.2 16 Qn3 Quebec nonchilled 175   32.4 17 Qn3 Quebec nonchilled 250   40.3 18 Qn3 Quebec nonchilled 350   42.i 19 Qn3 Quebec nonchilled 500   42.ix xx Qn3 Quebec nonchilled 675   43.9 21 Qn3 Quebec nonchilled 1000  45.5 22 Qc1 Quebec  chilled     95  xiv.ii 23 Qc1 Quebec  chilled    175  24.1 24 Qc1 Quebec  chilled    250  30.3 25 Qc1 Quebec  chilled    350  34.half dozen 26 Qc1 Quebec  chilled    500  32.v 27 Qc1 Quebec  chilled    675  35.iv 28 Qc1 Quebec  chilled   1000  38.seven 29 Qc2 Quebec  chilled    95   nine.3 30 Qc2 Quebec  chilled   175   27.3 31 Qc2 Quebec  chilled   250   35.0 32 Qc2 Quebec  chilled   350   38.8 33 Qc2 Quebec  chilled   500   38.half-dozen 34 Qc2 Quebec  chilled   675   37.five 35 Qc2 Quebec  chilled   1000  42.4 36 Qc3 Quebec  chilled   95    15.1 37 Qc3 Quebec  chilled   175   21.0 38 Qc3 Quebec  chilled   250   38.1 39 Qc3 Quebec  chilled   350   34.0 twoscore Qc3 Quebec  chilled   500   38.ix 41 Qc3 Quebec  chilled   675   39.half-dozen 42 Qc3 Quebec  chilled   thousand  41.4  $Mississippi Grouped Data: uptake ~ conc | Plant     Plant  Type     Treatment  conc uptake 43  Mn1 Mississippi nonchilled 95    10.half-dozen 44  Mn1 Mississippi nonchilled 175   19.2 45  Mn1 Mississippi nonchilled 250   26.2 46  Mn1 Mississippi nonchilled 350   30.0 47  Mn1 Mississippi nonchilled 500   30.9 48  Mn1 Mississippi nonchilled 675   32.4 49  Mn1 Mississippi nonchilled 1000  35.v 50  Mn2 Mississippi nonchilled 95    12.0 51  Mn2 Mississippi nonchilled 175   22.0 52  Mn2 Mississippi nonchilled 250   thirty.6 53  Mn2 Mississippi nonchilled 350   31.8 54  Mn2 Mississippi nonchilled 500   32.4 55  Mn2 Mississippi nonchilled 675   31.1 56  Mn2 Mississippi nonchilled 1000  31.5 57  Mn3 Mississippi nonchilled 95    11.3 58  Mn3 Mississippi nonchilled 175   xix.iv 59  Mn3 Mississippi nonchilled 250   25.8 60  Mn3 Mississippi nonchilled 350   27.9 61  Mn3 Mississippi nonchilled 500   28.v 62  Mn3 Mississippi nonchilled 675   28.1 63  Mn3 Mississippi nonchilled thou   27.eight 64  Mc1 Mississippi chilled    95     10.5 65  Mc1 Mississippi chilled    175    fourteen.9 66  Mc1 Mississippi chilled    250    18.one 67  Mc1 Mississippi chilled    350    xviii.9 68  Mc1 Mississippi chilled    500    19.5 69  Mc1 Mississippi chilled    675    22.2 seventy  Mc1 Mississippi chilled    1000   21.9 71  Mc2 Mississippi chilled    95     seven.vii 72  Mc2 Mississippi chilled    175    11.iv 73  Mc2 Mississippi chilled    250    12.three 74  Mc2 Mississippi chilled    350    thirteen.0 75  Mc2 Mississippi chilled    500    12.5 76  Mc2 Mississippi chilled    675    13.vii 77  Mc2 Mississippi chilled   1000    fourteen.4 78  Mc3 Mississippi chilled    95     10.6 79  Mc3 Mississippi chilled    175    18.0 80  Mc3 Mississippi chilled    250    17.ix 81  Mc3 Mississippi chilled    350    17.nine 82  Mc3 Mississippi chilled    500    17.ix 83  Mc3 Mississippi chilled    675    eighteen.9 84  Mc3 Mississippi chilled    g 19.9

raja

Published on 07-Aug-2021 08:12:26

  • Related Questions & Answers
  • How to create a chiselled variable using a data frame column in R?
  • How to create a frequency cavalcade for categorical variable in an R data frame?
  • How to find the summary by categorical variable in R?
  • How to find the sum based on a categorical variable in an R data frame?
  • How to convert all words of a string or chiselled variable in an R data frame to uppercase?
  • How to check if a column is categorical in R information frame?
  • How to convert a data frame with categorical columns to numeric in R?
  • How to filter rows by excluding a particular value in columns of the R data frame?
  • How to standardize columns if some columns are categorical in R data frame?
  • How to initialize a data frame with variable names in R?
  • Prepare values in categorical column to numeric values in R data frame.
  • How to find the mean of a numerical column past two categorical columns in an R information frame?
  • How to create a table of sums of a detached variable for 2 categorical variables in an R data frame?
  • How to subset an R data frame based on numerical and chiselled column?
  • How to carve up a information frame past column in R?

How To Filter Dataframe In R,

Source: https://www.tutorialspoint.com/how-to-filter-data-frame-by-categorical-variable-in-r

Posted by: hooksthislem.blogspot.com

0 Response to "How To Filter Dataframe In R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel