using lapply function and list in r

后端 未结 2 1641
别跟我提以往
别跟我提以往 2021-01-25 23:33
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), co         


        
2条回答
  •  生来不讨喜
    2021-01-26 00:07

    You need to iterate over data and counts simultaneously. In tidyverse I would recommend using purrr::map2(), but in base R you can simply do:'

    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"
    

提交回复
热议问题