How can I subset rows in a data frame in R based on a vector of values?

前端 未结 4 1887
挽巷
挽巷 2020-11-28 04:48

I have two data sets that are supposed to be the same size but aren\'t. I need to trim the values from A that are not in B and vice versa in order to eliminate noise from a

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 04:49

    Really human comprehensible example (as this is the first time I am using %in%), how to compare two data frames and keep only rows containing the equal values in specific column:

    # Set seed for reproducibility.
    set.seed(1)
    
    # Create two sample data frames.
    data_A <- data.frame(id=c(1,2,3), value=c(1,2,3))
    data_B <- data.frame(id=c(1,2,3,4), value=c(5,6,7,8))
    
    # compare data frames by specific columns and keep only 
    # the rows with equal values 
    data_A[data_A$id %in% data_B$id,]   # will keep data in data_A
    data_B[data_B$id %in% data_A$id,]   # will keep data in data_b
    

    Results:

    > data_A[data_A$id %in% data_B$id,]
      id value
    1  1     1
    2  2     2
    3  3     3
    
    > data_B[data_B$id %in% data_A$id,]
      id value
    1  1     5
    2  2     6
    3  3     7
    

提交回复
热议问题