简体   繁体   中英

How can filter Function be used between 2 datasets in R Studio?

If I generate a sample of 30 from a data frame that has 50 observations, how can I separate the remaining 20 from the data frame of 50 using filter function? Is it possible to use the filter function between two data frames? If so, then how?

Thanks in advance.

Here is an example:

# dummy data
dat <- data.frame(x = 1:10,
                  y = letters[1:10], stringsAsFactors = FALSE)

Create a sample index, set a seed for reproducibility.

set.seed(1)
idx <- sort(sample(1:nrow(dat), size = 6, replace = FALSE))
idx
#[1] 2 3 4 5 7 8

Subset your data frame

dat[idx, ]
#  x y
#2 2 b
#3 3 c
#4 4 d
#5 5 e
#7 7 g
#8 8 h

Get the rows that are not in idx

dat[-idx, ]
#    x y
#1   1 a
#6   6 f
#9   9 i
#10 10 j

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