在比较R中的两个数据帧之后提取缺失的元素

时间:2014-03-04 20:55:53

标签: r dataframe elements

所以我有一个包含96个元素的主列表。

masterlist <- list(c(paste0(rep(LETTERS[1:8], each=12), rep(1:12, 8))))

我有一个包含26个数据帧的列表,其中每个数据帧包含主列表中的任何或所有元素。我把这些叫做rowcol.list。从这里,我使用以下代码来查看我的26个数据帧列表中的哪些数据帧与主列表不同。

rowcol.list[!(rowcol.list%in%masterlist)]

上述代码从26个数据帧列表中返回10个数据帧,如下所示:

$p01_control.txt
 [1] "A1"  "A2"  "A3"  "A5"  "A6"  "A7"  "A8"  "A9"  "A10" "A11" "A12" "B1"  "B2"  "B3"  "B4"  "B5"  "B6"  "B7"  "B8"  "B9" 
[21] "B10" "B11" "B12" "C1"  "C2"  "C3"  "C4"  "C5"  "C6"  "C7"  "C8"  "C9"  "C10" "C11" "C12" "D1"  "D2"  "D3"  "D4"  "D5" 
[41] "D6"  "D7"  "D8"  "D9"  "D10" "D11" "D12" "E1"  "E2"  "E3"  "E4"  "E5"  "E6"  "E7"  "E8"  "E9"  "E10" "E11" "E12" "F1" 
[61] "F2"  "F3"  "F4"  "F5"  "F6"  "F7"  "F8"  "F9"  "F10" "F11" "F12" "G1"  "G2"  "G3"  "G4"  "G5"  "G6"  "G7"  "G8"  "G9" 
[81] "G10" "G11" "G12" "H1"  "H2"  "H3"  "H4"  "H5"  "H6"  "H7"  "H8"  "H9"  "H10" "H11" "H12"

$p03_control.txt
[1] "A1"  "A2"  "A3"  "A4"  "A5"  "A6"  "A7"  "A8"  "A9"  "A10" "A11" "A12" "B1"  "B2"  "B3"  "B4"  "B5"  "B6"  "B7"  "B8" 
[21] "B9"  "B10" "B11" "B12" "C1"  "C2"  "C3"  "C4"  "C5"  "C6"  "C7"  "C8"  "C9"  "C10" "C11" "C12" "D1"  "D2"  "D3"  "D4" 
[41] "D5"  "D6"  "D7"  "D8"  "D9"  "D10" "D11" "D12" "E1"  "E2"  "E3"  "E4"  "E5"  "E6"  "E7"  "E8"  "E9"  "E10" "E11" "E12" 
[61] "F1"  "F2"  "F3"  "F4"  "F5"  "F6"  "F7"  "F8"  "F9"  "F10" "F11" "F12" "G1"  "G2"  "G3"  "G4"  "G5"  "G6"  "G7"  "G9" 
[81] "G10" "G11" "G12" "H1"  "H2"  "H3"  "H4"  "H5"  "H6"  "H7"  "H8"  "H9"  "H10" "H11" "H12"

$p04_pq.txt
[1] "A1"  "A2"  "A3"  "A5"  "A6"  "A7"  "A8"  "A9"  "A10" "A11" "A12" "B1"  "B2"  "B3"  "B4"  "B5"  "B6"  "B7"  "B8"  "B9" 
[21] "B10" "B11" "B12" "C1"  "C2"  "C3"  "C4"  "C5"  "C6"  "C7"  "C8"  "C9"  "C10" "C11" "C12" "D1"  "D2"  "D3"  "D4"  "D5" 
[41] "D6"  "D7"  "D8"  "D9"  "D10" "D11" "D12" "E1"  "E2"  "E3"  "E4"  "E5"  "E6"  "E7"  "E8"  "E9"  "E10" "E11" "E12" "F1" 
[61] "F2"  "F3"  "F4"  "F5"  "F6"  "F7"  "F8"  "F9"  "F10" "F11" "F12" "G1"  "G2"  "G3"  "G4"  "G5"  "G6"  "G7"  "G8"  "G9" 
[81] "G10" "G11" "H1"  "H2"  "H3"  "H4"  "H5"  "H6"  "H7"  "H8"  "H9"  "H10" "H11" "H12"

$p07_docetaxel.txt
[1] "A1"  "A2"  "A3"  "A4"  "A5"  "A6"  "A7"  "A8"  "A9"  "A10" "A11" "A12" "B1"  "B2"  "B3"  "B4"  "B5"  "B6"  "B7"  "B8" 
[21] "B9"  "B10" "B11" "B12" "C1"  "C2"  "C3"  "C4"  "C5"  "C6"  "C7"  "C8"  "C9"  "C10" "C11" "C12" "D1"  "D2"  "D3"  "D4" 
[41] "D5"  "D6"  "D7"  "D8"  "D9"  "D10" "D11" "D12" "E1"  "E2"  "E3"  "E4"  "E5"  "E6"  "E7"  "E8"  "E9"  "E10" "E11" "E12"
[61] "F1"  "F2"  "F3"  "F4"  "F5"  "F6"  "F7"  "F8"  "F9"  "F10" "F11" "F12" "G1"  "G2"  "G3"  "G4"  "G5"  "G6"  "G7"  "G9" 
[81] "G10" "G11" "G12" "H1"  "H2"  "H3"  "H4"  "H5"  "H6"  "H7"  "H8"  "H9"  "H10" "H11" "H12"

$p08_docetaxel_b.txt
[1] "E10" "E11" "E12" "F1"  "F2"  "F3"  "F4"  "F5"  "F6"  "F7"  "F8"  "F9"  "F10" "F11" "F12" "G1"  "G2"  "G3"  "G4"  "G5" 
[21] "G6"  "G7"  "G8"  "G9"  "G10" "G11" "G12" "H1"  "H2"  "H3"  "H4"  "H5"  "H6"  "H7"  "H8"  "H9"  "H10" "H11" "H12"

$p08_docetaxel.txt
[1] "A1"  "A2"  "A3"  "A4"  "A5"  "A6"  "A7"  "A8"  "A9"  "A10" "A11" "A12" "B1"  "B2"  "B3"  "B4"  "B5"  "B6"  "B7"  "B8" 
[21] "B9"  "B10" "B11" "B12" "C1"  "C2"  "C3"  "C4"  "C5"  "C6"  "C7"  "C8"  "C9"  "C10" "C11" "C12" "D1"  "D2"  "D3"  "D4" 
[41] "D5"  "D6"  "D7"  "D8"  "D9"  "D10" "D11" "D12" "E1"  "E2"  "E3"  "E4"  "E5"  "E6"  "E7"  "E8" 

$p12_dactinomycin.txt
[1] "A1"  "A2"  "A3"  "A4"  "A5"  "A6"  "A7"  "A8"  "A9"  "A10" "A11" "A12" "B1"  "B2"  "B3"  "B4"  "B5"  "B6"  "B7"  "B8" 
[21] "B9"  "B10" "B11" "B12" "C1"  "C2"  "C3"  "C4"  "C5"  "C6"  "C7"  "C8"  "C9"  "C10" "C11" "C12" "D1"  "D2"  "D3"  "D4" 
[41] "D5"  "D6"  "D7"  "D8"  "D9"  "D10" "D11" "D12" "E1"  "E2"  "E3"  "E4"  "E5"  "E6"  "E7"  "E8"  "E9"  "E10" "E11" "F1" 
[61] "F2"  "F3"  "F4"  "F5"  "F6"  "F7"  "F8"  "F9"  "F10" "F11" "F12" "G1"  "G2"  "G3"  "G4"  "G5"  "G6"  "G7"  "G8"  "G9" 
[81] "G10" "G11" "G12" "H1"  "H2"  "H3"  "H4"  "H5"  "H6"  "H7"  "H8"  "H9"  "H10" "H11" "H12"

$p16_cisplatin.txt
[1] "A1"  "A2"  "A3"  "A4"  "A5"  "A6"  "A7"  "A8"  "A9"  "A10" "A11" "A12" "B1"  "B2"  "B3"  "B4"  "B5"  "B6"  "B7"  "B8" 
[21] "B9"  "B10" "B11" "B12" "C1"  "C2"  "C3"  "C4"  "C5"  "C6"  "C7"  "C8"  "C9"  "C10" "C11" "C12" "D1"  "D2"  "D3"  "D4" 
[41] "D5"  "D6"  "D7"  "D8"  "D9"  "D10" "D11" "D12" "E1"  "E2"  "E3"  "E4"  "E5"  "E6"  "E7"  "E8"  "E9"  "E10" "E11" "E12"
[61] "F1"  "F2"  "F3"  "F4"  "F5"  "F6"  "F7"  "F9"  "F10" "F11" "F12" "G1"  "G2"  "G3"  "G4"  "G5"  "G6"  "G7"  "G8"  "G9" 
[81] "G10" "G11" "G12" "H1"  "H2"  "H3"  "H4"  "H5"  "H6"  "H7"  "H8"  "H9"  "H10" "H11" "H12"

$p18_cisplatin_b.txt
[1] "A7"  "A8"  "A9"  "A10" "A11" "A12" "B1"  "B2"  "B3"  "B4"  "B5"  "B6"  "B7"  "B8"  "B9"  "B10" "B11" "B12" "C1"  "C2"  
[21] "C3"  "C4"  "C5"  "C6"  "C7"  "C8"  "C9"  "C10" "C11" "C12" "D1"  "D2"  "D3"  "D4"  "D5"  "D6"  "D7"  "D8"  "D9"  "D10"
[41] "D11" "D12" "E1"  "E2"  "E3"  "E4"  "E5"  "E6"  "E7"  "E8"  "E9"  "E10" "E11" "E12" "F1"  "F2"  "F3"  "F4"  "F5"  "F6" 
[61] "F7"  "F8"  "F9"  "F10" "F11" "F12" "G1"  "G2"  "G3"  "G4"  "G5"  "G6"  "G7"  "G8"  "G9"  "G10" "G11" "G12" "H1"  "H2" 
[81] "H3"  "H4"  "H5"  "H6"  "H7"  "H8"  "H9"  "H10" "H11" "H12"

$p18_cisplatin.txt
[1] "A1" "A2" "A3" "A4" "A5"

我希望看到数据框与主列表进行比较时没有哪些元素,而不是看到上面每个数据帧的元素都有,因为它们与主列表不同。作为一个简短的例子,第一个不同的数据框,$ p01_control.txt应该显示“A4”,因为这是它所缺少的。 (我只知道它缺少“A4”,因为我通过列表找到了哪一个丢失了)。

如果令人困惑,请告诉我如何让问题更加清晰。 谢谢!

1 个答案:

答案 0 :(得分:0)

我会使用setdiff。你可以尝试这样的事情:

lapply(rowcol.list,function(x) setdiff(masterlist[[1]],x))