R根据列表保留行

时间:2014-08-11 13:51:46

标签: r list

我试图根据我从其他数据框创建的列表来弄清楚如何保留数据帧的行。我已经查看了其他几个解决此问题的问题,并尝试了建议的代码(R- keep dataframe rows only if the row name is in a listSelect rows in a dataframe in r based on values in one row),但代码并不适用于我。我确定这是一个简单的错误,但我无法弄清楚它是什么。

我试过的一个代码:

   A <- AllSiteBA[AllSiteBA$Cofecha %in% keeps,]

输出(没有任何匹配......当我运行整个AllSiteBA data.frame时,应该有76个匹配):

> A    
[1] Cofecha     DBHinBark   RBHinBark   BAtotal     MeanBAww    PercentBAww
[7] Plot       
<0 rows> (or 0-length row.names)

我尝试过的另一个代码:

  

A&lt; - subset(AllSiteBA,Cofecha%in%keep)

与我尝试过的第一个代码完全相同。

任何有关如何修复代码的帮助将不胜感激。下面是我的data.frame和list的可重现部分。

&#34;保持&#34;:

的名称列表
keeps <- list(structure(c(69L, 166L, 50L, 232L, 252L, 234L, 148L, 307L, 
194L, 240L, 245L, 297L, 248L, 221L, 257L, 218L, 265L, 45L, 208L, 
216L, 223L, 258L, 205L, 269L, 270L, 142L, 4L, 58L, 207L, 220L, 
231L, 256L, 206L, 268L, 203L, 267L, 111L, 144L, 290L, 74L, 36L, 
266L, 169L, 78L, 143L, 186L, 272L, 185L, 312L, 73L, 150L, 129L, 
225L, 131L, 123L, 12L, 300L, 302L, 264L, 284L, 128L, 130L, 1L, 
3L, 10L, 124L, 303L, 56L, 51L, 55L, 108L, 2L, 11L, 298L, 310L, 
121L), .Label = c("LB1A002", "LB1A003", "LB1A101", "LB1A102", 
"LB1A103", "LB1A212", "LB1A228", "LB1A231", "LB1A233", "LB1B001", 
"LB1B002", "LB1B003", "LB1B210", "LB1B216", "LB2A001", "LB2A002", 
"LB2A003", "LB2A004", "LB2A008", "LB2A009", "LB2A011", "LB2B001", 
"LB2B005", "LB2B008", "LB2B101", "LB2B102", "LB2B103", "LB2C003", 
"LB2C004", "LB2C008", "LB2C009", "LB2C010", "LB2C001", "LB2D005", 
"LB2D006", "LB2D007", "LB2D008", "LB2D009", "LB2D010", "LB2D101", 
"SM1A005", "SM1A101", "SM1A301", "SM1A302", "SM1B003", "SM1C005", 
"SM1C302", "SM1D006", "SM2A004", "SM2A005", "SM2A007", "SM2A210", 
"SM2A301", "SM2B001", "SM2B005", "SM2B006", "SM2B101", "SM2C005", 
"SM2C101", "SM2C301", "SM2D006", "SM2D101", "SM2D221", "IR1A004", 
"IR1A009", "IR1A206", "IR1B001", "IR1B004", "IR1B005", "IR1B301", 
"IR1B302", "IR1C005", "IR1C006", "IR1C007", "IR1C008", "IR1C204", 
"IR1C205", "IR1D002", "IR1D101", "IR2A003", "IR2A101", "IR2A211", 
"IR2A234", "IR2B002", "IR2B005", "IR2B101", "IR2B201", "IR2B210", 
"IR2B229", "IR2C230", "IR2C256", "IR2C301", "IR2C302", "IR2C002", 
"IR2C009", "IR2C101", "IR2C204", "IR2C215", "IR2D227", "IR2D228", 
"IR2D237", "IR2D254", "IR2D301", "IR2D302", "IR2D003", "IR2D006", 
"IR2D009", "IR2D011", "IR2D207", "IR2D216", "JA1A101", "JA1A224", 
"JA1A301", "JA1B004", "JA1B101", "JA1B102", "JA1B219", "JA1B233", 
"JA1C002", "JA1C232", "JA1D001", "JA1D101", "JA2A101", "JA2A102", 
"JA2A206", "JA2A209", "JA2A210", "JA2A004", "JA2A005", "JA2A006", 
"JA2A007", "JA2A008", "JA2B005", "JA2B206", "JA2C001", "JA2C002", 
"JA2C007", "JA2C101", "JA2C202", "JA3N007", "JA3N008", "JA3N009", 
"JA3N010", "JA3N011", "JA3N012", "JA3N001", "JA3N002", "JA3N003", 
"JA3N004", "JA3N005", "JA3N006", "SF5A007", "SF5B223", "SF5B227", 
"SF5B228", "SF5B301", "SF5B302", "SF5C201", "SF5C214", "SF5C216", 
"SF5C301", "SF5C303", "SF5D004", "SF5D101", "SF5D207", "AP1A001", 
"AP1A004", "AP1A005", "AP1A006", "AP1A008", "AP1A009", "AP1A010", 
"AP1A101", "AP1B005", "AP1B007", "AP1B011", "AP1B101", "AP1B102", 
"AP1C006", "AP1C007", "AP1C010", "AP1C011", "AP1C001", "AP1C002", 
"AP1D001", "AP1D005", "AP1D007", "AP1D008", "AP1D009", "AP1D010", 
"AP1D011", "AP1D012", "AP1D013", "AP1D101", "AP1D102", "AP1D103", 
"AP1D104", "AP1C004", "AP1C005", "AP2A001", "AP2A002", "AP2A003", 
"AP2B001", "AP2B003", "AP2B004", "AP2B101", "AP2B102", "AP2C001", 
"AP2C002", "AP2C003", "AP2C004", "AP2C005", "AP2C007", "AP2C008", 
"AP2C102", "AP2C103", "AP2C104", "AP2D001", "AP2D002", "AP2D005", 
"AP2D006", "AP2D009", "AP2D101", "AP2D102", "AP2D103", "AP3A003", 
"AP3A005", "AP3A008", "AP3A014", "AP3A015", "AP3A101", "AP3A102", 
"AP3B101", "AP3B102", "AP3B103", "AP3B104", "AP3B003", "AP3B007", 
"AP3B010", "AP3B012", "AP3C003", "AP3C004", "AP3C006", "AP3C007", 
"AP3C009", "AP3C011", "AP3C101", "AP3C102", "AP3C103", "AP3C104", 
"AP3C105", "AP3D006", "AP3D011", "AP3D101", "AP3D102", "BF1A101", 
"BF1A102", "BF1A103", "BF1A104", "BF1B003", "BF1B005", "BF1B006", 
"BF1B007", "BF1B101", "BF1C007", "BF1C101", "BF1C102", "BF1D003", 
"BF1D007", "BF1D010", "BF1D101", "BF1D102", "BF1D103", "BF1D210", 
"BF2A001", "BF2A002", "BF2B001", "BF2B214", "BF2B219", "BF2C001", 
"BF2C004", "BF2C008", "BF2C101", "BF2C102", "BF2C201", "BF2C205", 
"BF2C213", "BF2C219", "BF2C301", "BF2D004", "BF2D013", "BF2D014", 
"BF2D015", "BF3A001", "BF3A002", "BF3A004", "BF3A005", "BF3A007", 
"BF3A008", "BF3A009", "BF3A101", "BF3B003", "BF3B101", "BF3C002", 
"BF3C003", "BF3C007", "BF3C009", "BF3C010", "BF3D002", "BF3D003", 
"BF3D004", "BF3D009", "BF3D010"), class = "factor"))

data.frame的一小部分我尝试从以下位置选择行:

AllSiteBA <- structure(list(Cofecha = structure(30:45, .Label = c("LB1A002", 
"LB1A003", "LB1A101", "LB1A102", "LB1A103", "LB1A212", "LB1A228", 
"LB1A231", "LB1A233", "LB1B001", "LB1B002", "LB1B003", "LB1B210", 
"LB1B216", "LB2A001", "LB2A002", "LB2A003", "LB2A004", "LB2A008", 
"LB2A009", "LB2A011", "LB2B001", "LB2B005", "LB2B008", "LB2B101", 
"LB2B102", "LB2B103", "LB2C001", "LB2C003", "LB2C004", "LB2C008", 
"LB2C009", "LB2C010", "LB2D005", "LB2D006", "LB2D007", "LB2D008", 
"LB2D009", "LB2D010", "LB2D101", "SM1A005", "SM1A101", "SM1A301", 
"SM1A302", "SM1B003", "SM1C005", "SM1C302", "SM1D006", "SM2A004", 
"SM2A005", "SM2A007", "SM2A210", "SM2A301", "SM2B001", "SM2B005", 
"SM2B006", "SM2B101", "SM2C005", "SM2C101", "SM2C301", "SM2D006", 
"SM2D101", "SM2D221", "IR1A004", "IR1A009", "IR1A206", "IR1B001", 
"IR1B004", "IR1B005", "IR1B301", "IR1B302", "IR1C005", "IR1C006", 
"IR1C007", "IR1C008", "IR1C204", "IR1C205", "IR1D002", "IR1D101", 
"IR2A003", "IR2A101", "IR2A211", "IR2A234", "IR2B002", "IR2B005", 
"IR2B101", "IR2B201", "IR2B210", "IR2B229", "IR2C002", "IR2C009", 
"IR2C101", "IR2C204", "IR2C215", "IR2C230", "IR2C256", "IR2C301", 
"IR2C302", "IR2D003", "IR2D006", "IR2D009", "IR2D011", "IR2D207", 
"IR2D216", "IR2D227", "IR2D228", "IR2D237", "IR2D254", "IR2D301", 
"IR2D302", "JA1A101", "JA1A224", "JA1A301", "JA1B004", "JA1B101", 
"JA1B102", "JA1B219", "JA1B233", "JA1C002", "JA1C232", "JA1D001", 
"JA1D101", "JA2A004", "JA2A005", "JA2A006", "JA2A007", "JA2A008", 
"JA2A101", "JA2A102", "JA2A206", "JA2A209", "JA2A210", "JA2B005", 
"JA2B206", "JA2C001", "JA2C002", "JA2C007", "JA2C101", "JA2C202", 
"JA3N001", "JA3N002", "JA3N003", "JA3N004", "JA3N005", "JA3N006", 
"JA3N007", "JA3N008", "JA3N009", "JA3N010", "JA3N011", "JA3N012", 
"SF5A007", "SF5B223", "SF5B227", "SF5B228", "SF5B301", "SF5B302", 
"SF5C201", "SF5C214", "SF5C216", "SF5C301", "SF5C303", "SF5D004", 
"SF5D101", "SF5D207", "AP1A001", "AP1A004", "AP1A005", "AP1A006", 
"AP1A008", "AP1A009", "AP1A010", "AP1A101", "AP1B005", "AP1B007", 
"AP1B011", "AP1B101", "AP1B102", "AP1C001", "AP1C002", "AP1C004", 
"AP1C005", "AP1C006", "AP1C007", "AP1C010", "AP1C011", "AP1D001", 
"AP1D005", "AP1D007", "AP1D008", "AP1D009", "AP1D010", "AP1D011", 
"AP1D012", "AP1D013", "AP1D101", "AP1D102", "AP1D103", "AP1D104", 
"AP2A001", "AP2A002", "AP2A003", "AP2B001", "AP2B003", "AP2B004", 
"AP2B101", "AP2B102", "AP2C001", "AP2C002", "AP2C003", "AP2C004", 
"AP2C005", "AP2C007", "AP2C008", "AP2C102", "AP2C103", "AP2C104", 
"AP2D001", "AP2D002", "AP2D005", "AP2D006", "AP2D009", "AP2D101", 
"AP2D102", "AP2D103", "AP3A003", "AP3A005", "AP3A008", "AP3A014", 
"AP3A015", "AP3A101", "AP3A102", "AP3B003", "AP3B007", "AP3B010", 
"AP3B012", "AP3B101", "AP3B102", "AP3B103", "AP3B104", "AP3C003", 
"AP3C004", "AP3C006", "AP3C007", "AP3C009", "AP3C011", "AP3C101", 
"AP3C102", "AP3C103", "AP3C104", "AP3C105", "AP3D006", "AP3D011", 
"AP3D101", "AP3D102", "BF1A101", "BF1A102", "BF1A103", "BF1A104", 
"BF1B003", "BF1B005", "BF1B006", "BF1B007", "BF1B101", "BF1C007", 
"BF1C101", "BF1C102", "BF1D003", "BF1D007", "BF1D010", "BF1D101", 
"BF1D102", "BF1D103", "BF1D210", "BF2A001", "BF2A002", "BF2B001", 
"BF2B214", "BF2B219", "BF2C001", "BF2C004", "BF2C008", "BF2C101", 
"BF2C102", "BF2C201", "BF2C205", "BF2C213", "BF2C219", "BF2C301", 
"BF2D004", "BF2D013", "BF2D014", "BF2D015", "BF3A001", "BF3A002", 
"BF3A004", "BF3A005", "BF3A007", "BF3A008", "BF3A009", "BF3A101", 
"BF3B003", "BF3B101", "BF3C002", "BF3C003", "BF3C007", "BF3C009", 
"BF3C010", "BF3D002", "BF3D003", "BF3D004", "BF3D009", "BF3D010"
), class = "factor"), DBHinBark = c(144, 147.6, 135.9, 144, 163.8, 
119.7, 234.9, 180.9, 144.9, 202.5, 152.1, 180, 184.5, 68.4, 88.2, 
231.3), RBHinBark = c(72, 73.8, 67.95, 72, 81.9, 59.85, 117.45, 
90.45, 72.45, 101.25, 76.05, 90, 92.25, 34.2, 44.1, 115.65), 
    BAtotal = c(16286.0163162095, 17110.4958922176, 14505.3694541364, 
    16286.0163162095, 21072.5782991454, 11253.2555709933, 43336.7077139261, 
    25702.0056715304, 16490.2276926745, 32206.2334378166, 18169.7231252836, 
    25446.9004940773, 26735.14983159, 3674.53243134477, 6109.80080862797, 
    42018.5582683328), MeanBAww = c(7287.19846816407, 3511.25221054135, 
    5836.77552643544, 3226.29613334421, 6580.83174422834, 1240.15336040198, 
    15513.5106521598, 4648.51222574233, 4555.16310970877, 4909.94773909597, 
    1791.58819676346, 6095.75422479859, 2391.72518367973, 3568.38437887589, 
    3398.70860742085, 7723.82631584503), PercentBAww = c(44.7451256751543, 
    20.5210429473197, 40.2387236318961, 19.8102228973765, 31.2293619262301, 
    11.0203962984599, 35.7976216249915, 18.0861847326233, 27.6234094192182, 
    15.2453336357263, 9.86029442721902, 23.9548004135802, 8.9459950617284, 
    97.1112500854964, 55.62715895126, 18.3819403476917), Plot = c("LB2", 
    "LB2", "LB2", "LB2", "LB2", "LB2", "LB2", "LB2", "LB2", "LB2", 
    "LB2", "SM1", "SM1", "SM1", "SM1", "SM1")), .Names = c("Cofecha", 
"DBHinBark", "RBHinBark", "BAtotal", "MeanBAww", "PercentBAww", 
"Plot"), row.names = 30:45, class = "data.frame")

1 个答案:

答案 0 :(得分:1)

我得到了两行,但只有在我对keeps的第一个元素进行了子集后才有list

> AllSiteBA[AllSiteBA$Cofecha %in% keeps[[1]],]
   Cofecha DBHinBark RBHinBark  BAtotal  MeanBAww PercentBAww Plot
36 LB2D007     234.9    117.45 43336.71 15513.511    35.79762  LB2
45 SM1B003     231.3    115.65 42018.56  7723.826    18.38194  SM1