简体   繁体   中英

subsetting columns of a tibble with a character vector from another tibble

I have two tibbles

>a.tibble

names other
 a      0
 c      0
 d      0

>b.tibble

a   b   c   d  e
1   1   1   1  1
1   1   1   1  1

I would like to use names <- select(a.tibble,names) to subset b.tibble to make c.tibble :

>c.tibble

a   c   d  
1   1   1   
1   1   1 

It is important that I subset by names because in reality I have many names to subset a large tibble. This makes it so the usual select(b.tibble,-c(b,e)) or other "manual" entry of column names is not possible.

Data

library(tibble)
a.tibble = tibble(names = c("a","c","d"), other = 0)
b.tibble = tibble(a = rep(1,2), b = 1, c = 1, d = 1, e = 1)

@ytk有一个对我b.tibble[, names(b.tibble) %in% a.tibble$names]的答案b.tibble[, names(b.tibble) %in% a.tibble$names]

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