This is one way to do it. First I get the indices at which x is either 8 or 9. Then we can verify that at those indices, x is indeed 8 and 9.
> inds <- which(x %in% c(8,9))
> inds
[1] 1 3 4 12 15 19
> x[inds]
[1] 8 9 9 8 9 8
More Related Contents:
- R memory management / cannot allocate vector of size n Mb
- Test if a vector contains a given element
- Counting the number of elements with the values of x in a vector
- How to find common elements from multiple vectors?
- List distinct values in a vector in R
- Convert a dataframe to a vector (by rows)
- Reshaping data.frame from wide to long format
- Quickly reading very large tables as dataframes
- Combine two data frames by rows (rbind) when they have different sets of columns
- Formatting Decimal places in R
- ggplot2 line chart gives “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?”
- How to name variables on the fly?
- Cleaning up factor levels (collapsing multiple levels/labels)
- Plot multiple columns on the same graph in R [duplicate]
- How to cbind or rbind different lengths vectors without repeating the elements of the shorter vectors?
- How to merge 2 vectors alternating indexes?
- Multiply rows of matrix by vector?
- Convert UNIX epoch to Date object
- Use variable names in functions of dplyr
- Access variable value where the name of variable is stored in a string
- How to index a vector sequence within a vector sequence
- Converting two columns of a data frame to a named vector
- calculating mean for every n values from a vector
- Find start and end positions/indices of runs/consecutive values
- Display exact value of a variable in R
- Is there anything wrong with using T & F instead of TRUE & FALSE?
- Split delimited single value character vector
- R split numeric vector at position
- How to return 5 topmost values from vector in R?
- How to reverse order a vector?