简体   繁体   English

使用for循环在R中创建数据帧列表

[英]using a for loop to create a list of data frames in R

I have a list of 26 data frames. 我有26个数据帧的列表。 I'm using the code below to create a vector. 我正在使用下面的代码创建矢量。 Is there a way I can use the for loop to apply the same unique function on all the 26 data frames and have a new list of 26 data frames with the vector in each data frame? 有没有一种方法可以使用for循环在所有26个数据帧上应用相同的唯一函数,并使用26个数据帧的新列表以及每个数据帧中的向量? Each data frame would create a different vector depending on that data frame. 每个数据帧将根据该数据帧创建不同的向量。

(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"

So the result I want is something like... 所以我想要的结果是...

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

Clarification 澄清度

Each data frame contains any or all of the elements that the following list contains (the following list has 96 elements): 每个数据帧包含以下列表包含的任何或所有元素(以下列表包含96个元素):

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

So I need something that would go through my list of 26 data frames and tell me the elements that each data frame contains. 因此,我需要一些可以遍历我的26个数据帧的列表,并告诉我每个数据帧包含的元素。 Because some of the data frames may not have all 96 elements, I showed that [[3]] in my example of result I want only has only like.. 40 elements. 因为某些数据帧可能没有全部96个元素,所以我在我想要的结果示例中显示[[3]]仅具有40个元素。 Hope that's more clear now.. 希望现在更加清楚。

something like: 就像是:

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

should do it. 应该这样做。 do.call calls its first argument (in this case the function paste0 ) with the arguments set to do.call 's second argument. do.call调用其第一个参数(在本例中为函数paste0 ),并将参数设置为do.call的第二个参数。 Since x is a data frame, which is a list, this works nicely. 由于x是一个数据帧(它是一个列表),因此效果很好。 Here is the sample output: 这是示例输出:

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

And the data I used: 和我使用的数据:

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))

I didn't understand the structure of your data, so please pardon my fake data sets. 我不了解您的数据结构,因此请原谅我的假数据集。

You can also use transform within the lapply function. 您也可以在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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM