首页 文章

如何根据值向量对R中数据帧中的行进行子集?

提问于
浏览
40

我有两个数据集应该是相同的大小但不是 . 我需要修剪A中不在B中的值,反之亦然,以消除进入报告的图形中的噪声 . (别担心,这些数据没有被永久删除!)

我看过以下内容:

但是我仍然无法使其正常工作 . 这是我的代码:

bg2011missingFromBeg <- setdiff(x=eg2011$ID, y=bg2011$ID)
#attempt 1
eg2011cleaned <- subset(eg2011, ID != bg2011missingFromBeg)
#attempt 2
eg2011cleaned <- eg2011[!eg2011$ID %in% bg2011missingFromBeg]

第一次尝试只是消除了生成的setdiff向量中的第一个值 . 第二次尝试产生并且笨拙的错误:

Error in `[.data.frame`(eg2012, !eg2012$ID %in% bg2012missingFromBeg) 
:  undefined columns selected

4 回答

  • 1

    根据对原始帖子的评论,合并/连接非常适合此问题 . 特别是,内部联接将仅返回两个数据框中存在的值,从而不需要 setdiff 语句 .

    使用Dinre的例子中的数据:

    In base R:

    cleanedA <- merge(data_A, data_B[, "index"], by = 1, sort = FALSE)
    cleanedB <- merge(data_B, data_A[, "index"], by = 1, sort = FALSE)
    

    Using the dplyr package:

    library(dplyr)
    cleanedA <- inner_join(data_A, data_B %>% select(index))
    cleanedB <- inner_join(data_B, data_A %>% select(index))
    

    要将数据保存为两个单独的表,每个表只包含自己的变量,这会将不需要的表子集设置为仅在其加入之前的索引变量 . 然后,没有新的变量添加到结果表中 .

  • 60

    这会给你你想要的:

    eg2011cleaned <- eg2011[!eg2011$ID %in% bg2011missingFromBeg, ]
    

    你第二次尝试的错误是因为你忘记了 ,

    通常,为方便起见,规范 object[index] subsets列为2d object . 如果要对行进行子集并保留所有列,则必须使用规范 object[index_rows, index_columns] ,而 index_cols 可以留空,默认情况下将使用所有列 .

    但是,您仍需要包含 , 以指示您要获取行的子集而不是列的子集 .

  • 15

    如果您真的只想通过两个数据帧中存在的索引对每个数据帧进行子集化,则可以使用“匹配”功能执行此操作,如下所示:

    data_A[match(data_B$index, data_A$index, nomatch=0),]
    data_B[match(data_A$index, data_B$index, nomatch=0),]
    

    但是,这与:

    data_A[data_A$index %in% data_B$index,]
    data_B[data_B$index %in% data_A$index,]
    

    这是一个演示:

    # Set seed for reproducibility.
    set.seed(1)
    
    # Create two sample data sets.
    data_A <- data.frame(index=sample(1:200, 90, rep=FALSE), value=runif(90))
    data_B <- data.frame(index=sample(1:200, 120, rep=FALSE), value=runif(120))
    
    # Subset data of each data frame by the index in the other.
    t_A <- data_A[match(data_B$index, data_A$index, nomatch=0),]
    t_B <- data_B[match(data_A$index, data_B$index, nomatch=0),]
    
    # Make sure they match.
    data.frame(t_A[order(t_A$index),], t_B[order(t_B$index),])[1:20,]
    
    #    index     value index.1    value.1
    # 27     3 0.7155661       3 0.65887761
    # 10    12 0.6049333      12 0.14362694
    # 88    14 0.7410786      14 0.42021589
    # 56    15 0.4525708      15 0.78101754
    # 38    18 0.2075451      18 0.70277874
    # 24    23 0.4314737      23 0.78218212
    # 34    32 0.1734423      32 0.85508236
    # 22    38 0.7317925      38 0.56426384
    # 84    39 0.3913593      39 0.09485786
    # 5     40 0.7789147      40 0.31248966
    # 74    43 0.7799849      43 0.10910096
    # 71    45 0.2847905      45 0.26787813
    # 57    46 0.1751268      46 0.17719454
    # 25    48 0.1482116      48 0.99607737
    # 81    53 0.6304141      53 0.26721208
    # 60    58 0.8645449      58 0.96920881
    # 30    59 0.6401010      59 0.67371223
    # 75    61 0.8806190      61 0.69882454
    # 63    64 0.3287773      64 0.36918946
    # 19    70 0.9240745      70 0.11350771
    
  • 2

    实际 human comprehensible 示例(因为这是我第一次使用%in%),如何比较两个数据帧并仅保留特定列中包含相等值的行:

    # 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
    

    结果:

    > 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
    

相关问题