【问题标题】:Create a list of dataframes/data.tables which created from function with argument in R?创建从 R 中带有参数的函数创建的 dataframes/data.tables 列表?
【发布时间】:2021-12-16 08:48:09
【问题描述】:

R 代码使用函数和循环的效率给我留下了深刻的印象。
我将首先提供问题的简化示例,并解释我的问题(代码可能无法复制)。

如果我有多个内容和长度不同的向量,例如:

tables_vector_1 <- c(1,2,3)
tables_vector_2 <- c(1:10)

我有一个从向量创建 data.tables 的函数,例如:

create_dt <- function(tables_vector, i){
  DT <- data.table(id = 1:i,  name = c("a","b","c"))
  return(DT)
}

我想知道,如果有办法编写循环或函数,我可以通过运行之前创建的函数在向量中创建所有(或部分)data.tables?
(可能喜欢)

for i in 1:length(tables_vector){
   create_dt(tables_vector, i)
}

然后将结果合并到一个列表中,与运行时的结果相同:

list(create_dt(tables_vector_1,1),create_dt(tables_vector_1,2),create_dt(tables_vector_1,3))

我曾尝试使用lapply(list(1:3),create_dt,tables_vector = tables_vector_1, i),但它失败了,因为我不知道如何在lapply() 中正确指定i 参数。


这是为什么会出现此问题的解释:
在实际情况中,我创建了一个从数据库中导入 data.table 的函数:

import_data <- function(tables_vector,i){ 
  end <- Sys.time()
  start <- end - 7200
  con <- dbConnect("PostgreSQL", dbname="db", host = "host", user=db_user, password=db_password)
  query <- sprintf("SELECT %s.timeutc, %s.scal AS %s FROM %s WHERE timeutc BETWEEN '%s' AND '%s' AND mode='General';",
                   tables_vector[i],tables_vector[i],tables_vector[i], tables_vector[i],start,end)
  rs <- dbSendQuery(con, query)
  df <- fetch(rs, n = -1)
  dbClearResult(rs)
  dbDisconnect(con)
  return(as.data.table(df))
}

我有数十个由组定义的向量(例如,vector1 包含用于用途 1 的通道,vector2 包含用于用途 2 的通道)。
由于它们是为不同的分析目的而创建的,因此我不能简单地将它们组合在一个向量中。

而且,有些向量包含7、8个通道,所以通过一个一个重复函数来列出它们是很烦人的。

【问题讨论】:

  • 您的实际用例很可能只需要data.table::rbindlist 并使用idcol 参数。但很难确定。

标签: r loops data.table


【解决方案1】:

这样的事情怎么样:

tables_vector_1 <- c(1,2,3)
tables_vector_2 <- c(1:10)

create_dt <- function(tables_vector, i){
  DT <- data.table(id = 1:i,  name = letters[1:i])
  return(DT)
}

make_list <- function(x){
  lapply(seq_along(x), function(i)create_dt(x, i))  
}

make_list(tables_vector_1)
[[1]]
   id name
1:  1    a

[[2]]
   id name
1:  1    a
2:  2    b

[[3]]
   id name
1:  1    a
2:  2    b
3:  3    c

make_list(tables_vector_2)
[[1]]
   id name
1:  1    a

[[2]]
   id name
1:  1    a
2:  2    b

[[3]]
   id name
1:  1    a
2:  2    b
3:  3    c

[[4]]
   id name
1:  1    a
2:  2    b
3:  3    c
4:  4    d

[[5]]
   id name
1:  1    a
2:  2    b
3:  3    c
4:  4    d
5:  5    e

[[6]]
   id name
1:  1    a
2:  2    b
3:  3    c
4:  4    d
5:  5    e
6:  6    f

[[7]]
   id name
1:  1    a
2:  2    b
3:  3    c
4:  4    d
5:  5    e
6:  6    f
7:  7    g

[[8]]
   id name
1:  1    a
2:  2    b
3:  3    c
4:  4    d
5:  5    e
6:  6    f
7:  7    g
8:  8    h

[[9]]
   id name
1:  1    a
2:  2    b
3:  3    c
4:  4    d
5:  5    e
6:  6    f
7:  7    g
8:  8    h
9:  9    i

[[10]]
    id name
 1:  1    a
 2:  2    b
 3:  3    c
 4:  4    d
 5:  5    e
 6:  6    f
 7:  7    g
 8:  8    h
 9:  9    i
10: 10    j

请注意,我更改了 create_dt() 函数,因此它不会产生警告,但机制仍应按预期工作。

【讨论】:

  • 谢谢!效果很好。
猜你喜欢
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多