使用lapply函数和r中的列表

问题描述 投票:1回答:2
d1 <- data.frame(col_one = c(1,2,3),col_two = c(4, 5, 6))
d2 <- data.frame(col_one = c(1, 1, 1), col_two = c(6, 5, 4))
d3 <- data.frame(col_one = c(7, 1, 1), col_two = c(8, 5, 4))
my.list <- list(d1, d2,d3)

for (i in 1:3) {
  table<- lapply(my.list, function(data, count) {
    sql <-
      #sqldf(
        paste0(
          "select *,count(col_one) from data where col_one = ",
          count," group by col_one"
        )
      #)
    print(sql)
  },
  count = i)
}

输出:

[1] "select *,count(col_one) from data where col_one = 1 group by col_one"
[1] "select *,count(col_one) from data where col_one = 1 group by col_one"
[1] "select *,count(col_one) from data where col_one = 1 group by col_one"
[1] "select *,count(col_one) from data where col_one = 2 group by col_one"
[1] "select *,count(col_one) from data where col_one = 2 group by col_one"
[1] "select *,count(col_one) from data where col_one = 2 group by col_one"
[1] "select *,count(col_one) from data where col_one = 3 group by col_one"
[1] "select *,count(col_one) from data where col_one = 3 group by col_one"
[1] "select *,count(col_one) from data where col_one = 3 group by col_one"

期望:

[1] "select *,count(col_one) from data where col_one = 1 group by col_one"
[1] "select *,count(col_one) from data where col_one = 2 group by col_one"
[1] "select *,count(col_one) from data where col_one = 3 group by col_one"

我怎么能改进?我希望运行SQL来创建我想要的新数据集,但它不成功,我可以指定知道与SQL语句相关的列表索引。还有另一个简单的方法吗?

我尝试过其中一种方法。

d1 <- data.frame(col_one = c(1,2,3),col_two = c(4, 5, 6))
d2 <- data.frame(col_one = c(3, 2, 1), col_two = c(6, 5, 4))
d3 <- data.frame(col_one = c(7, 2, 1), col_two = c(8, 5, 4))
my.list <- list(d1, d2,d3)
seq_along(x)
#for (i in 1:3) {
  table<- lapply(seq_along(my.list), function(index) {
    sql <-
      sqldf(
        paste0(
          "select *,count(col_one) from my.list where col_one = ",
          index," group by col_one"
        )
      )
    print(sql)
  })
#}

输出:

[1] "select *,count(col_one) from my.list where col_one = 1 group by col_one"
[1] "select *,count(col_one) from my.list where col_one = 2 group by col_one"
[1] "select *,count(col_one) from my.list where col_one = 3 group by col_one"

但是,它不会找到运行SQL的数据集。

d1 <- data.frame(col_one = c(1,2,3),col_two = c(4, 5, 6))
d2 <- data.frame(col_one = c(1, 1, 1), col_two = c(6, 5, 4))
d3 <- data.frame(col_one = c(7, 1, 1), col_two = c(8, 5, 4))
my.list <- list(d1, d2,d3)
table<- mapply(function(data, count) {
  sql <-
    sqldf(
    paste0(
      "select *,count(col_one) from data where col_one = ",
      count," group by col_one"
    )
  )
  print(sql)
}, my.list, 1
)
r apply lapply
2个回答
1
投票

如果我理解正确,OP想要为col_one中的每个data.frames创建my.list的列联表,即,他想知道每个数据中每个值1,2或3出现在col_one中的次数。帧。

正如my answeranother question of the OP所解释的那样,并且正如G. Grothendieck所建议的那样,将data.frames与大型data.table中的相同结构相结合几乎总是更好,而不是将它们分开放在列表中。顺便说一句,OP还有第三个question ("how to loop the dataframe using sqldf?")请求帮助查看data.frames列表。

要在大型data.table中组合data.frames,使用rbindlist()函数。请注意,添加的id列df标识每行的原始data.frame。

library(data.table)
rbindlist(my.list, idcol = "df")
   df col_one col_two
1:  1       1       4
2:  1       2       5
3:  1       3       6
4:  2       1       6
5:  2       1       5
6:  2       1       4
7:  3       7       8
8:  3       1       5
9:  3       1       4

现在我们可以轻松计算聚合:

rbindlist(my.list, idcol = "df")[, count_col_one := .N, by = .(df, col_one)][]
   df col_one col_two count_col_one
1:  1       1       4             1
2:  1       2       5             1
3:  1       3       6             1
4:  2       1       6             3
5:  2       1       5             3
6:  2       1       4             3
7:  3       7       8             1
8:  3       1       5             2
9:  3       1       4             2

这个data.table语句通过使用特殊符号col_onedf.N分组来计算每个dfcol_one中每个单独值的出现次数。

在这个问题中,OP只要求计算col_one中1,2或3的出现次数。如果确实如此,则需要删除值7。这可以通过过滤结果来完成:

rbindlist(my.list, idcol = "df")[, count_col_one := .N, by = .(df, col_one)][
  col_one %in% 1:3]

1
投票

你需要同时迭代datacounts。在tidyverse中,我建议使用purrr :: map2(),但在基数R中,您可以简单地执行:'

table<- mapply(function(data, count) {
    sql <-
      #sqldf(
      paste0(
        "select *,count(col_one) from data where col_one = ",
        count," group by col_one"
      )
    #)
    print(sql)
  }, my.list, 1:3
  )
[1] "select *,count(col_one) from data where col_one = 1 group by col_one"
[1] "select *,count(col_one) from data where col_one = 2 group by col_one"
[1] "select *,count(col_one) from data where col_one = 3 group by col_one"
© www.soinside.com 2019 - 2024. All rights reserved.