2014-02-27 139 views
0

我有一個包含26個數據幀的列表。我使用下面的代碼來創建一個向量。有沒有一種方法可以使用for循環在所有26個數據幀上應用相同的唯一函數,並在每個數據幀中有一個包含矢量的26個數據幀的新列表?每個數據幀將根據該數據幀創建不同的矢量。使用for循環在R中創建數據幀列表

(unique(paste(list[[1]]$row, list[[1]]$col, sep="")) 

> (unique(paste(list[[1]]$row, list[[1]]$col, sep=""))) 
[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" 

所以結果我想是一樣的東西......

[[1]] 
[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" 

[[2]] 
[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" 

[[3]] 
[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" 

etc etc until [[26]] 

澄清

每個數據幀中包含的任何或全部以下列表中包含的元素(以下列表有96個元素):

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

所以我需要的東西會通過我的26個數據幀的列表,並告訴我每個數據幀包含的元素。因爲某些數據框可能沒有全部96個元素,所以我在我的結果示例中顯示[[3]]只有像.. 40個元素。希望現在更清楚..

+1

使用'lapply'。它專爲這些任務而設計。 – Roland

+0

@羅蘭這樣的事情? lapply(list,function(x)unique(paste(list [[i]] $ row,list [[i]] $ col,sep =「」)))它給了我現在列表的數據框,它的返回與原始列表的每個數據幀都不相符。 – hj14

+1

差不多。 'lapply(list,function(x)unique(paste0(x $ row,x $ col)))'' – Roland

回答

0

類似:

lapply(list, function(x) unique(do.call(paste0, x))) 

應該這樣做。 do.call使用參數設置爲do.call的第二個參數來調用其第一個參數(在本例中爲函數paste0)。由於x是一個數據框,這是一個列表,這很好地工作。下面是示例輸出:

[[1]] 
[1] "G18" "J20" "N12" "U11" "E1" 

[[2]] 
[1] "F10" "E14" "Q18" "I7" "X13" 

[[3]] 
[1] "Y8" "F1" "P7" "C15" "Z6" 

[[4]] 
[1] "M14" "O16" "L2" "E13" "S7" 

[[5]] 
[1] "V16" "Q1" "S9" "M13" "L12" 

和數據I使用:

set.seed(1) 
list <- replicate(5, data.frame(row=sample(LETTERS, 5), col=sample(1:20, 5)), s=F) 
lapply(list, function(x) do.call(paste0, x)) 
0

我不明白您的數據的結構,所以請原諒我的假數據集。

您還可以在lapply函數中使用transform

df1 <- data.frame(a = c(1, 2, 3), 
        b = c(4, 5, 6)) 

df2 <- data.frame(a = c(5, 6, 7), 
        b = c(8, 9, 10)) 

dfList <- list(df1 = df1, df2 = df2) 

lapply(dfList, transform, c = a - b)