r - Subset data.frame based on a column that matches a separate vector -


this question has answer here:

once again stumped here.

i have data.frame of 4 columns:

col1      col2      col3      col4 1         1.lsm     0.43      0.34 2         1.lsm     0.47      0.30 3         1.lsm     0.27      0.85 1         2.lsm     0.35      0.55 2         2.lsm     0.71      0.46 3         2.lsm     0.53      0.37 4         2.lsm     0.63      0.34 

col1 cell number cells have been tracked on time.

i have vector containing integers pertaining cells want keep:

keep=c(3, 4) 

now, want use vector "keep" decide rows of data.frame kept , output new data.frame columns keep relevant rows.

i.e. ideal output here be:

col1      col2      col3      col4 3         1.lsm     0.27      0.85 3         2.lsm     0.53      0.37 4         2.lsm     0.63      0.34 

you can try

 df[df$col1 %in% keep, ]  #   col1  col2 col3 col4  # 3    3 1.lsm 0.27 0.85  # 6    3 2.lsm 0.53 0.37  # 7    4 2.lsm 0.63 0.34 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -