简体   繁体   中英

How to extract frequency from R table using vector of arguments

I have a table, for example:

tbl <- table(iris[, 1:3])

I can extract frequency from the table this way:

tbl[['4.6', '3.6', '1']]

How to do the same if my values stored as vector

vars <- c('4.6', '3.6', '1')

In fact vars is named vector and I need general way to extract from tables.

You can convert vars to matrix and use it to subset tbl :

tbl[t(vars)]
#[1] 1

We can also use

tbl[matrix(vars, ncol = length(vars))]
#[1] 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM