簡體   English   中英

使用lapply函數並在r中列出

[英]using lapply function and list in r

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
)

您需要同時遍歷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"

如果我理解正確,則OP想要創建應急表為col_one每個在data.frames的my.list ,即,他想知道有多少次,每次值的1,2,或3出現在col_one在每個data.frame。

正如 在對OP的另一個問題的 回答中所解釋的那樣,並且正如G. Grothendieck所建議的那樣,將具有相同結構的data.frames組合在一個大型data.table中幾乎總是比將它們保持在列表中更好。 順便說一句,OP還提出了第三個問題(“如何使用sqldf循環數據框?”) ,以尋求有關data.frames列表的幫助。

要將data.frame組合到一個大的data.table中,請使用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語句通過使用特殊符號.N並按dfcol_one進行分組來col_one針對每個df col_one中每個單獨值的出現。

在問題中,OP僅要求對col_one出現的1、2或3進行col_one 如果確實要這樣做,則需要刪除7的值。 這可以通過過濾結果來完成:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM