根据R中的阿姆斯特丹条件选择列并创建新的数据框

我想从主数据中过滤公司的数据基础。尽管我可以使用过滤器,但是我想使用诸如'if'或'for'或'within'之类的条件来构建代码,因为我的函数中需要此代码块。我尝试在函数中使用来自dplyr的过滤器,但出现错误

Error in tbl_if_vars(.tbl, .p, .env, ..., .include_group_vars = .include_group_vars) : 
  length(.p) == length(tibble_vars) is not TRUE

以下数据供您参考:

structure(list(COMPANY = c(1804L, 1804L, 1804L, 1804L, 1804L, 
1804L, 1804L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 
2203L, 2203L, 2203L, 2203L, 2203L, 2203L, 2203L), SUPPLIER = structure(c(4L, 
4L, 4L, 4L, 4L, 5L, 5L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L), .Label = c("168600", "1804", "1805A", "1816", 
"2121"), class = "factor"), NAME = structure(c(1L, 1L, 1L, 1L, 
1L, 5L, 5L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 
4L), .Label = c("CETES Cosmetics AG", "CETES Cosmetics Poland Sp. z o.o.", 
"HCP Packaging Hong Kong Limited", "Oriflame Cosmetics Global S.A., Luxembourg, Schaffhausen Branch", 
"ORIFLAME SOFTWARE S.R.O"), class = "factor"), INVOICE_NO = structure(c(18L, 
17L, 19L, 20L, 21L, 13L, 14L, 1L, 4L, 2L, 5L, 6L, 3L, 7L, 15L, 
16L, 9L, 8L, 10L, 11L, 12L), .Label = c("1044308", "31994", "32206", 
"32315", "32378", "32461", "ADV-AGST-0000031995", "G27361", "G27414", 
"G27416", "G27436", "G27444", "II2017000258_VAT", "II2017000683_VAT", 
"II2018034", "II2018137", "II2201664_CORR", "II2201665_CORRR", 
"II2201764_VAT", "II2202165", "II2202956"), class = "factor"), 
    INVOICE_ID = c(2274780L, 2274784L, 2291616L, 2381865L, 2605862L, 
    1934047L, 2291611L, 2291290L, 1869956L, 1854405L, 1893341L, 
    1893350L, 1854606L, 1908630L, 2006709L, 2280038L, 1898273L, 
    1895932L, 1899501L, 1899519L, 1900665L), INVOICE_AMOUNT = structure(c(5L, 
    1L, 4L, 6L, 3L, 7L, 9L, 15L, 17L, 13L, 10L, 11L, 14L, 2L, 
    19L, 18L, 8L, 20L, 12L, 21L, 16L), .Label = c("-159,247.00", 
    "1,381.56", "120,871.00", "179,618.00", "182,381.00", "199,281.00", 
    "23,288.00", "237,634.09", "27,657.00", "298.43", "4,501.47", 
    "4,774.80", "5,073.42", "5,101.68", "50,268.46", "6,843.50", 
    "615", "67,354.44", "76,193.59", "9,364.89", "9,570.60"), class = "factor"), 
    CURRENCY = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("EUR", 
    "PLN", "USD"), class = "factor"), INVOICE_DATE = structure(c(12L, 
    12L, 7L, 5L, 1L, 13L, 11L, 11L, 15L, 3L, 4L, 10L, 6L, 8L, 
    9L, 11L, 16L, 14L, 16L, 2L, 2L), .Label = c("10/12/2019", 
    "12/12/2017", "13-09-2017", "14-11-2017", "18-04-2019", "18-10-2017", 
    "18-12-2018", "21-12-2017", "22-03-2018", "27-11-2017", "28-12-2018", 
    "30-11-2018", "31-12-2017", "4/12/2017", "6/11/2017", "8/12/2017"
    ), class = "factor"), DUE_DATE = structure(c(15L, 15L, 7L, 
    8L, 18L, 5L, 2L, 12L, 16L, 3L, 4L, 11L, 6L, 10L, 9L, 13L, 
    17L, 14L, 17L, 1L, 1L), .Label = c("11/1/2018", "11/1/2019", 
    "12/12/2017", "12/2/2018", "14-01-2018", "16-01-2018", "17-01-2019", 
    "18-05-2019", "21-04-2018", "21-12-2017", "25-02-2018", "26-02-2019", 
    "27-01-2019", "3/1/2018", "30-12-2018", "4/2/2018", "7/1/2018", 
    "9/1/2020"), class = "factor"), AMOUNT_PAID = structure(c(5L, 
    1L, 4L, 6L, 3L, 7L, 9L, 15L, 17L, 13L, 10L, 11L, 14L, 2L, 
    19L, 18L, 8L, 20L, 12L, 21L, 16L), .Label = c("-159,247.00", 
    "1,381.56", "120,871.00", "179,618.00", "182,381.00", "199,281.00", 
    "23,288.00", "237,634.09", "27,657.00", "298.43", "4,501.47", 
    "4,774.80", "5,073.42", "5,101.68", "50,268.46", "6,843.50", 
    "615", "67,354.44", "76,193.59", "9,364.89", "9,570.60"), class = "factor"), 
    INVOICE_AMT = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 
    9L, 7L, 3L, 6L, 8L, 2L, 13L, 11L, 4L, 14L, 10L, 15L, 12L), .Label = c("", 
    "104,587.55", "22,876.15", "237634.09", "3,473,550.59", "345,060.18", 
    "389,784.77", "391,955.95", "46,322.11", "4774.8", "67354.44", 
    "6843.5", "76193.59", "9364.89", "9570.6"), class = "factor"), 
    INVOCE_AMT_EUR = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    11L, 15L, 12L, 9L, 10L, 13L, 3L, 6L, 5L, 14L, 7L, 2L, 8L, 
    4L), .Label = c("", "1,135.83", "1,381.56", "1,626.42", "15,675.12", 
    "18,029.29", "2,221.59", "2,276.66", "298.43", "4,501.47", 
    "41,797.89", "5,073.42", "5,101.68", "56,544.54", "615"), class = "factor"), 
    POSTING_DATE = structure(c(12L, 12L, 7L, 10L, 6L, 8L, 7L, 
    1L, 16L, 13L, 5L, 17L, 2L, 9L, 11L, 14L, 3L, 15L, 4L, 4L, 
    5L), .Label = c("1/4/2019", "10/1/2018", "11/12/2017", "12/12/2017", 
    "13-12-2017", "13-12-2019", "15-01-2019", "17-01-2018", "21-12-2017", 
    "22-04-2019", "23-03-2018", "27-12-2018", "30-10-2017", "31-12-2018", 
    "7/12/2017", "8/12/2017", "8/2/2018"), class = "factor"), 
    PAYMENT_MODE = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("", 
    "ORI_GLB"), class = "factor"), PAY_TERM_ID = c(30L, 30L, 
    30L, 30L, 30L, 14L, 14L, 60L, 90L, 90L, 90L, 90L, 90L, 0L, 
    30L, 30L, 30L, 30L, 30L, 30L, 30L)), class = "data.frame", row.names = c(NA, 
-21L))

输出应根据以下代码:

A1804<-mydata%>%filter(COMPANY==1804)%>%select(SUPPLIER,INVOICE_NO)
A2002<-mydata%>%filter(COMPANY==2002)%>%select(SUPPLIER,INVOICE_ID)
A2203<-mydata%>%filter(COMPANY==2203)%>%select(SUPPLIER,AMOUNT_PAID,DUE_DATE)
评论