How to subset R dataframe based on specific values in several columns?

I would like to subset an R dataframe to display all the rows that contain specific values that could be sitting in two or more columns. Suppose I’ve got dataframe df

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> HomeTeam AwayTeam
1 Barcelona Ajax
2 Madrid Liverpool
3 Arsenal Barcelona
4 Madrid Barcelona
5 ManU Arsenal
6 Ajax Porto
</code>
<code> HomeTeam AwayTeam 1 Barcelona Ajax 2 Madrid Liverpool 3 Arsenal Barcelona 4 Madrid Barcelona 5 ManU Arsenal 6 Ajax Porto </code>
   HomeTeam    AwayTeam
1 Barcelona      Ajax
2   Madrid    Liverpool
3   Arsenal   Barcelona
4   Madrid    Barcelona
5    ManU       Arsenal
6    Ajax       Porto

And I want to display only the rows which contain “Barcelona” or “Arsenal”, i.e. I want to get

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> HomeTeam AwayTeam
1 Barcelona Ajax
2 Arsenal Barcelona
3 Madrid Barcelona
4 ManU Arsenal
</code>
<code> HomeTeam AwayTeam 1 Barcelona Ajax 2 Arsenal Barcelona 3 Madrid Barcelona 4 ManU Arsenal </code>
   HomeTeam     AwayTeam
1  Barcelona      Ajax
2   Arsenal     Barcelona
3   Madrid      Barcelona
4    ManU        Arsenal

I attempted to subset the dataframe and incorporate OR condition || like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df = df[df$HomeTeam %in% c("Barcelona", "Arsenal")
|| df$AwayTeam %in% c("Barcelona", "Arsenal"), ]
</code>
<code>df = df[df$HomeTeam %in% c("Barcelona", "Arsenal") || df$AwayTeam %in% c("Barcelona", "Arsenal"), ] </code>
df = df[df$HomeTeam %in% c("Barcelona", "Arsenal") 
                   || df$AwayTeam %in% c("Barcelona", "Arsenal"), ]

But it gives me an error. What am I doing wrong and how to achieve the desired outcome?

1

As mentioned in the comments | is vectorized but || is not so your code would work if you make that change or you can try the other alternatives below, some of which are a bit shorter.

Also it is normally best not to clobber df as in df <- df[...] but rather create a new data frame df2 <- df[...]. This will make it easier to debug since you will then know that df is always the input and also df and df2 will both exist at the same time so you can compare them which can be useful during debugging.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># 1
df[df$HomeTeam %in% c("Barcelona", "Arsenal") |
df$AwayTeam %in% c("Barcelona", "Arsenal"), ]
# 2
targets <- c("Barcelona", "Arsenal")
subset(df, HomeTeam %in% targets | AwayTeam %in% targets)
# 3
df[rowSums(df == "Arsenal" | df == "Barcelona") > 0, ]
# 4
subset(df, rowSums(df == "Arsenal" | df == "Barcelona") > 0)
# 5
pat <- "Arsenal|Barcelona"
subset(df, grepl(pat, HomeTeam) | grepl(pat, AwayTeam))
</code>
<code># 1 df[df$HomeTeam %in% c("Barcelona", "Arsenal") | df$AwayTeam %in% c("Barcelona", "Arsenal"), ] # 2 targets <- c("Barcelona", "Arsenal") subset(df, HomeTeam %in% targets | AwayTeam %in% targets) # 3 df[rowSums(df == "Arsenal" | df == "Barcelona") > 0, ] # 4 subset(df, rowSums(df == "Arsenal" | df == "Barcelona") > 0) # 5 pat <- "Arsenal|Barcelona" subset(df, grepl(pat, HomeTeam) | grepl(pat, AwayTeam)) </code>
# 1
df[df$HomeTeam %in% c("Barcelona", "Arsenal") | 
   df$AwayTeam %in% c("Barcelona", "Arsenal"), ]

# 2
targets <- c("Barcelona", "Arsenal")
subset(df, HomeTeam %in% targets | AwayTeam %in% targets)

# 3
df[rowSums(df == "Arsenal" | df == "Barcelona") > 0, ]

# 4
subset(df, rowSums(df == "Arsenal" | df == "Barcelona") > 0)

# 5
pat <- "Arsenal|Barcelona"
subset(df, grepl(pat, HomeTeam) | grepl(pat, AwayTeam))

Solid answer by @G. Grothendieck, so this is just another option:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df <- data.frame(HomeTeam=c('Barcelona','Madrid','Arsenal','Madrid','ManU','Ajax'),
AwayTeam=c('Ajax','Liverpool','Barcelona','Barcelona','Arsenal','Porto'))
df %>%
dplyr::filter((HomeTeam == "Barcelona" | HomeTeam == "Arsenal") |
(AwayTeam == "Barcelona" | AwayTeam == "Arsenal"))
HomeTeam AwayTeam
1 Barcelona Ajax
2 Arsenal Barcelona
3 Madrid Barcelona
4 ManU Arsenal
</code>
<code>df <- data.frame(HomeTeam=c('Barcelona','Madrid','Arsenal','Madrid','ManU','Ajax'), AwayTeam=c('Ajax','Liverpool','Barcelona','Barcelona','Arsenal','Porto')) df %>% dplyr::filter((HomeTeam == "Barcelona" | HomeTeam == "Arsenal") | (AwayTeam == "Barcelona" | AwayTeam == "Arsenal")) HomeTeam AwayTeam 1 Barcelona Ajax 2 Arsenal Barcelona 3 Madrid Barcelona 4 ManU Arsenal </code>
df <- data.frame(HomeTeam=c('Barcelona','Madrid','Arsenal','Madrid','ManU','Ajax'),
AwayTeam=c('Ajax','Liverpool','Barcelona','Barcelona','Arsenal','Porto'))

df %>% 
  dplyr::filter((HomeTeam == "Barcelona" | HomeTeam == "Arsenal") | 
                (AwayTeam == "Barcelona" | AwayTeam == "Arsenal")) 

HomeTeam  AwayTeam
1 Barcelona      Ajax
2   Arsenal Barcelona
3    Madrid Barcelona
4      ManU   Arsenal

Cheers!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How to subset R dataframe based on specific values in several columns?

I would like to subset an R dataframe to display all the rows that contain specific values that could be sitting in two or more columns. Suppose I’ve got dataframe df

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> HomeTeam AwayTeam
1 Barcelona Ajax
2 Madrid Liverpool
3 Arsenal Barcelona
4 Madrid Barcelona
5 ManU Arsenal
6 Ajax Porto
</code>
<code> HomeTeam AwayTeam 1 Barcelona Ajax 2 Madrid Liverpool 3 Arsenal Barcelona 4 Madrid Barcelona 5 ManU Arsenal 6 Ajax Porto </code>
   HomeTeam    AwayTeam
1 Barcelona      Ajax
2   Madrid    Liverpool
3   Arsenal   Barcelona
4   Madrid    Barcelona
5    ManU       Arsenal
6    Ajax       Porto

And I want to display only the rows which contain “Barcelona” or “Arsenal”, i.e. I want to get

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> HomeTeam AwayTeam
1 Barcelona Ajax
2 Arsenal Barcelona
3 Madrid Barcelona
4 ManU Arsenal
</code>
<code> HomeTeam AwayTeam 1 Barcelona Ajax 2 Arsenal Barcelona 3 Madrid Barcelona 4 ManU Arsenal </code>
   HomeTeam     AwayTeam
1  Barcelona      Ajax
2   Arsenal     Barcelona
3   Madrid      Barcelona
4    ManU        Arsenal

I attempted to subset the dataframe and incorporate OR condition || like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df = df[df$HomeTeam %in% c("Barcelona", "Arsenal")
|| df$AwayTeam %in% c("Barcelona", "Arsenal"), ]
</code>
<code>df = df[df$HomeTeam %in% c("Barcelona", "Arsenal") || df$AwayTeam %in% c("Barcelona", "Arsenal"), ] </code>
df = df[df$HomeTeam %in% c("Barcelona", "Arsenal") 
                   || df$AwayTeam %in% c("Barcelona", "Arsenal"), ]

But it gives me an error. What am I doing wrong and how to achieve the desired outcome?

1

As mentioned in the comments | is vectorized but || is not so your code would work if you make that change or you can try the other alternatives below, some of which are a bit shorter.

Also it is normally best not to clobber df as in df <- df[...] but rather create a new data frame df2 <- df[...]. This will make it easier to debug since you will then know that df is always the input and also df and df2 will both exist at the same time so you can compare them which can be useful during debugging.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># 1
df[df$HomeTeam %in% c("Barcelona", "Arsenal") |
df$AwayTeam %in% c("Barcelona", "Arsenal"), ]
# 2
targets <- c("Barcelona", "Arsenal")
subset(df, HomeTeam %in% targets | AwayTeam %in% targets)
# 3
df[rowSums(df == "Arsenal" | df == "Barcelona") > 0, ]
# 4
subset(df, rowSums(df == "Arsenal" | df == "Barcelona") > 0)
# 5
pat <- "Arsenal|Barcelona"
subset(df, grepl(pat, HomeTeam) | grepl(pat, AwayTeam))
</code>
<code># 1 df[df$HomeTeam %in% c("Barcelona", "Arsenal") | df$AwayTeam %in% c("Barcelona", "Arsenal"), ] # 2 targets <- c("Barcelona", "Arsenal") subset(df, HomeTeam %in% targets | AwayTeam %in% targets) # 3 df[rowSums(df == "Arsenal" | df == "Barcelona") > 0, ] # 4 subset(df, rowSums(df == "Arsenal" | df == "Barcelona") > 0) # 5 pat <- "Arsenal|Barcelona" subset(df, grepl(pat, HomeTeam) | grepl(pat, AwayTeam)) </code>
# 1
df[df$HomeTeam %in% c("Barcelona", "Arsenal") | 
   df$AwayTeam %in% c("Barcelona", "Arsenal"), ]

# 2
targets <- c("Barcelona", "Arsenal")
subset(df, HomeTeam %in% targets | AwayTeam %in% targets)

# 3
df[rowSums(df == "Arsenal" | df == "Barcelona") > 0, ]

# 4
subset(df, rowSums(df == "Arsenal" | df == "Barcelona") > 0)

# 5
pat <- "Arsenal|Barcelona"
subset(df, grepl(pat, HomeTeam) | grepl(pat, AwayTeam))

Solid answer by @G. Grothendieck, so this is just another option:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df <- data.frame(HomeTeam=c('Barcelona','Madrid','Arsenal','Madrid','ManU','Ajax'),
AwayTeam=c('Ajax','Liverpool','Barcelona','Barcelona','Arsenal','Porto'))
df %>%
dplyr::filter((HomeTeam == "Barcelona" | HomeTeam == "Arsenal") |
(AwayTeam == "Barcelona" | AwayTeam == "Arsenal"))
HomeTeam AwayTeam
1 Barcelona Ajax
2 Arsenal Barcelona
3 Madrid Barcelona
4 ManU Arsenal
</code>
<code>df <- data.frame(HomeTeam=c('Barcelona','Madrid','Arsenal','Madrid','ManU','Ajax'), AwayTeam=c('Ajax','Liverpool','Barcelona','Barcelona','Arsenal','Porto')) df %>% dplyr::filter((HomeTeam == "Barcelona" | HomeTeam == "Arsenal") | (AwayTeam == "Barcelona" | AwayTeam == "Arsenal")) HomeTeam AwayTeam 1 Barcelona Ajax 2 Arsenal Barcelona 3 Madrid Barcelona 4 ManU Arsenal </code>
df <- data.frame(HomeTeam=c('Barcelona','Madrid','Arsenal','Madrid','ManU','Ajax'),
AwayTeam=c('Ajax','Liverpool','Barcelona','Barcelona','Arsenal','Porto'))

df %>% 
  dplyr::filter((HomeTeam == "Barcelona" | HomeTeam == "Arsenal") | 
                (AwayTeam == "Barcelona" | AwayTeam == "Arsenal")) 

HomeTeam  AwayTeam
1 Barcelona      Ajax
2   Arsenal Barcelona
3    Madrid Barcelona
4      ManU   Arsenal

Cheers!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật