The R which() function is used to return the index positions of elements that satisfy a condition.
\\\\nwhich() is very commonly used in data filtering and locating specific elements.
\\\\nThe syntax of the which() function is as follows:
\\\\nwhich(x, arr.ind = FALSE)\\\\n\\\\nParameter Description:
\\\\n- \\\\n
- x: A logical vector or an expression containing logical conditions. \\\\n
- arr.ind: When used for matrix/array, whether to return row and column indices, default FALSE. \\\\n
# Find the indices where Score is greater than or equal to Index of the highest score0\\\\nscores <- c(Get the corresponding Score by indexGet the corresponding Score by index, Index of the highest scoreFind the indices where Score is greater than or equal to Index of the highest score0, Index of the lowest scoreFind the row and column positions of elements greater than 8, Find the row and column positions of elements greater than 8Get the corresponding Score by index, column)Find the indices where Score is greater than or equal to Index of the highest score0, Index of the lowest score0, Find the row and column positions of elements greater than 8Find the row and column positions of elements greater than 8, column)Get the corresponding Score by index, Get the corresponding Score by index0, Find the row and column positions of elements greater than 8Find the indices where Score is greater than or equal to Index of the highest score0)\\\\npass_idx <- which(scores >= Index of the highest score0)\\\\n\\\\nprint("Indices of passing students:")\\\\nprint(pass_idx)\\\\n\\\\n# Get the corresponding score by index\\\\nprint("Scores of passing students:")\\\\nprint(scores)\\\\n\\\\n# Find the position of the maximum value\\\\nmax_idx <- which.max(scores)\\\\nprint(paste("Index of the highest score:", max_idx, "Score:", scores))\\\\n\\\\n# Find the position of the minimum value\\\\nmin_idx <- which.min(scores)\\\\nprint(paste("Index of the lowest score:", min_idx, "Score:", scores))\\\\n\\\\nThe output of the above code is:
\\\\n "Indices of passing students:"\\\\n Find the indices where Score is greater than or equal to Index of the highest score0 Find the position of the minimum value Scores of passing students Get the corresponding Score by index Index of the highest score Index of the lowest score Find the row and column positions of elements greater than 8 Find the position of the maximum value0\\\\n "Scores of passing students:"\\\\n Index of the highest scoreFind the indices where Score is greater than or equal to Index of the highest score0 Index of the lowest scoreFind the row and column positions of elements greater than 8 Find the row and column positions of elements greater than 8Get the corresponding Score by index column)Find the indices where Score is greater than or equal to Index of the highest score0 Index of the lowest score0 Find the row and column positions of elements greater than 8Find the row and column positions of elements greater than 8 column)Get the corresponding Score by index Find the row and column positions of elements greater than 8Find the indices where Score is greater than or equal to Index of the highest score0\\\\n "Index of the highest score: Find the row and column positions of elements greater than 8 Score: column)Get the corresponding Score by index"\\\\n "Index of the lowest score: Find the position of the maximum value Score: Get the corresponding Score by indexGet the corresponding Score by index"\\\\n\\\\nwhich() also works with matrices, and using arr.ind = TRUE can return row and column coordinates:
\\\\n# Create a Matrix\\\\nm <- matrix(Find the position of the maximum value:Find the position of the maximum valueFind the indices where Score is greater than or equal to Index of the highest score0, nrow=Find the position of the minimum value)\\\\nprint("Matrix:")\\\\nprint(m)\\\\n\\\\n# Findgreater than Find the row and column positions of elements greater than 8 ofelementsoflinecolumnpositions\\\\npositions <- which(m > Find the row and column positions of elements greater than 8, arr.ind= TRUE)\\\\nprint("Positions of elements greater than Find the row and column positions of elements greater than 8 (line, column):")\\\\nprint(positions)\\\\n\\\\nThe output of the above code is:
\\\\n "Matrix:"\\\\n [,Find the position of the maximum value] [,Find the indices where Score is greater than or equal to Index of the highest score0] [,Find the position of the minimum value] [,Scores of passing students]\\\\n[Find the position of the maximum value,] Find the position of the maximum value Scores of passing students Index of the lowest score Find the position of the maximum value0\\\\n[Find the indices where Score is greater than or equal to Index of the highest score0,] Find the indices where Score is greater than or equal to Index of the highest score0 Get the corresponding Score by index Find the row and column positions of elements greater than 8 Find the position of the maximum valueFind the position of the maximum value\\\\n[Find the position of the minimum value,] Find the position of the minimum value Index of the highest score column) Find the position of the maximum valueFind the indices where Score is greater than or equal to Index of the highest score0\\\\n "Positions of elements greater than Find the row and column positions of elements greater than 8 (line, column)οΌ"\\\\n row col\\\\n[Find the position of the maximum value,] Find the position of the maximum value Scores of passing students\\\\n[Find the indices where Score is greater than or equal to Index of the highest score0,] Find the indices where Score is greater than or equal to Index of the highest score0 Scores of passing students\\\\n[Find the position of the minimum value,] Find the position of the minimum value Find the position of the minimum value\\\\n[Scores of passing students,] Find the position of the minimum value Scores of passing students\\\\n\\\\n
YouTip