How To Use Which In R

You can use the following methods to use the which() function with multiple conditions in R:

Method 1: which() with Multiple Conditions Using AND

new_df <- df[which(df$my_column >= 14 & df$my_column <= 25), ]

Method 2: which() with Multiple Conditions Using OR

new_df <- df[which(df$my_column < 14 | df$my_column > 25), ]

The following examples show how to use each method in practice with the following data frame:

#create data frame df <- data.frame(player=c(‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’), points=c(10, 13, 13, 15, 19, 22, 24, 25, 29, 35)) #view data frame df player points 1 A 10 2 B 13 3 C 13 4 D 15 5 E 19 6 F 22 7 G 24 8 H 25 9 I 29 10 J 35

Example 1: which() with Multiple Conditions Using AND

The following code shows how to use the which() function to filter the data frame to only contain rows where the value in the points column is greater than or equal to 14 and less than or equal to 25:

#filter for players who score between 14 and 25 points new_df <- df[which(df$points >= 14 & df$points <= 25), ] #view results new_df player points 4 D 15 5 E 19 6 F 22 7 G 24 8 H 25

Refer to more articles:  Which Pokemon Drop Herba Mystica

Notice that the data frame is filtered to only contain rows where the value in the points column is greater than or equal to 14 and less than or equal to 25.

Note that the & operator is used as an “and” statement in R.

Example 2: which() with Multiple Conditions Using OR

The following code shows how to use the which() function to filter the data frame to only contain rows where the value in the points column is less than 14 or greater than 25:

Otherwise it assigns a value of “bad”:

#filter for players who score less than 14 or greater than 25 points new_df <- df[which(df$points < 14 | df$points > 25), ] #view results new_df player points 1 A 10 2 B 13 3 C 13 9 I 29 10 J 35

Notice that the data frame is filtered to only contain rows where the value in the points column is less than 14 or greater than 25.

Note that the | operator is used as an “or” statement in R.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use If Statement with Multiple Conditions in R How to Write a Nested If Else Statement in R How to Write a Nested For Loop in R

Related Posts

Which Nfl Kicker Won Mvp

Which Nfl Kicker Won Mvp

You may be interested Which Way Does A Saw Blade Go Which Css Property Configures The Font Typeface Which Two Aspects Of The Continuous Delivery Pipeline Which…

Which Witch Is Which Lyrics

[Sparkle sound] Two little witches (two little witches) Played a little game (played a little game) They were very good friends (very good friends) Who looked the…

Which Of The Following Is A Cause Of Gynecological Emergencies

Surgical Management of Ectopic Pregnancy In the shocked patient with intraperitoneal bleeding, which is the typical presentation in the tropics, laparotomy should be undertaken. In subacute presentation…

Which Is The Beautiful Airport In The World

Which Is The Beautiful Airport In The World

Airports are more than just gateways to our destinations; they can also be architectural marvels and serene retreats. Some airports around the globe are designed to impress,…

Which Of The Following Is True Regarding Project Methodologies

Which Of The Following Is True Regarding Project Methodologies

Learning Objectives After completing this module, you will be able to:You may be interested Which Wig Brands Run Large Which Is Better Kojic Acid Or Glycolic Acid…

Which Puss In Boots The Last Wish Character Are You

“Puss in Boots,” a spin-off of the iconic movie “Shrek,” is primarily remembered for its humor and adventures. It’s also a story of friendship, bravery, and understanding…