How to select a set of the whole tables by only selecting the name

Here is my example of the dataset

df <- data.frame(team=rep(c('A', 'B', 'C', 'D'), each=4),
pos=rep(c('G', 'F'), times=8),
points=round(runif(16, 4, 20),0))
image
How do I only select team A's data(with its pos and points) only using the name and without using selecting numeric values?

Thank you so much!

Hi @Janicehau,
This is a very common operation in R, and here is one way to achieve it:

suppressPackageStartupMessages(library(tidyverse))

df <- data.frame(team=rep(c('A', 'B', 'C', 'D'), each=4),
                 pos=rep(c('G', 'F'), times=8),
                 points=round(runif(16, 4, 20),0))
df
#>    team pos points
#> 1     A   G     13
#> 2     A   F     12
#> 3     A   G     15
#> 4     A   F      7
#> 5     B   G     18
#> 6     B   F     15
#> 7     B   G      7
#> 8     B   F      6
#> 9     C   G     17
#> 10    C   F      8
#> 11    C   G      6
#> 12    C   F      4
#> 13    D   G      5
#> 14    D   F     19
#> 15    D   G      8
#> 16    D   F      9

df %>% 
  filter(team == "A") -> teamA_only.df

teamA_only.df
#>   team pos points
#> 1    A   G     13
#> 2    A   F     12
#> 3    A   G     15
#> 4    A   F      7

Created on 2023-01-20 with reprex v2.0.2

1 Like
mtcars[which(mtcars$cyl == 8),]
#>                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
#> Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#> Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
#> Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#> Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
#> Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
#> Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
#> AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
#> Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
#> Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
#> Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
#> Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.