我有26个数据框的列表。我正在使用下面的代码创建一个向量。有没有办法可以使用for循环在所有26个数据帧上应用相同的唯一函数,并在每个数据帧中有一个带有向量的26个数据帧的新列表?每个数据帧将根据该数据帧创建不同的向量。
(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个元素。希望现在更清楚..
答案 0 :(得分:0)
类似的东西:
lapply(list, function(x) unique(do.call(paste0, x)))
应该这样做。 do.call
调用其第一个参数(在本例中为函数paste0
),其参数设置为do.call
的第二个参数。由于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"
我使用的数据:
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))
答案 1 :(得分:0)
我不了解您的数据结构,所以请原谅我的假数据集。
您还可以在transform
功能中使用lapply
。
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)