Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 跨多个列表重叠计算_R - Fatal编程技术网

R 跨多个列表重叠计算

R 跨多个列表重叠计算,r,R,这个问题有两个相似的部分: 问题1: 我有一个阿里巴巴的2个数据帧列表,字母表。我正在为名单上的两家公司做计算。因此,例如,对于列表中的所有列表,以销售、一般和管理/营业收入为例 我试图获得各公司历年(或者仅2017-03年)的具体比率输出 BS数据: BS <- structure(list(Alibaba = structure(list(V1 = c("Fiscal year ends in March. CNY in millions except per share data."

这个问题有两个相似的部分:

问题1:

我有一个阿里巴巴的2个数据帧列表,字母表。我正在为名单上的两家公司做计算。因此,例如,对于列表中的所有列表,以销售、一般和管理/营业收入为例

我试图获得各公司历年(或者仅2017-03年)的具体比率输出

BS
数据:

BS <- structure(list(Alibaba = structure(list(V1 = c("Fiscal year ends in March. CNY in millions except per share data.", 
"Assets", "Current assets", "Cash", "Cash and cash equivalents", 
"Short-term investments", "Total cash", "Receivables", "Inventories", 
"Deferred income taxes", "Prepaid expenses", "Other current assets", 
"Total current assets", "Non-current assets", "Property, plant and equipment", 
"Land", "Fixtures and equipment", "Other properties", "Property and equipment, at cost", 
"Accumulated Depreciation", "Property, plant and equipment, net", 
"Equity and other investments", "Goodwill", "Intangible assets", 
"Deferred income taxes", "Other long-term assets", "Total non-current assets", 
"Total assets", "Liabilities and stockholders' equity", "Liabilities", 
"Current liabilities", "Short-term debt", "Accounts payable", 
"Taxes payable", "Accrued liabilities", "Deferred revenues", 
"Other current liabilities", "Total current liabilities", "Non-current liabilities", 
"Long-term debt", "Deferred taxes liabilities", "Accrued liabilities", 
"Deferred revenues", "Minority interest", "Other long-term liabilities", 
"Total non-current liabilities", "Total liabilities", "Stockholders' equity", 
"Common stock", "Other Equity", "Additional paid-in capital", 
"Retained earnings", "Treasury stock", "Accumulated other comprehensive income", 
"Total stockholders' equity", "Total liabilities and stockholders' equity"
), V2 = c("2018-03", "", "", "", "199309", "10901", "210210", 
"25370", "4535", "", "5760", "10980", "256855", "", "", "", "35909", 
"51019", "86928", "-20439", "66489", "177892", "162149", "36842", 
"2182", "14715", "460269", "717124", "", "", "", "6028", "", 
"16071", "66930", "22297", "24484", "135810", "", "119525", "19312", 
"", "993", "70616", "5046", "215492", "351302", "", "1", "3854", 
"186764", "172353", "-2233", "5083", "365822", "717124"), V3 = c("2017-03", 
"", "", "", "143736", "7065", "150801", "21374", "", "652", "4176", 
"5513", "182516", "", "", "", "19311", "13156", "32467", "-12261", 
"20206", "151820", "125420", "18799", "386", "7665", "324296", 
"506812", "", "", "", "14897", "", "7674", "36025", "15052", 
"20123", "93771", "", "76835", "10154", "", "641", "42330", "4282", 
"134242", "228013", "", "1", "3393", "164585", "108558", "-2823", 
"5085", "278799", "506812"), V4 = c("2016-03", "", "", "", "106818", 
"8878", "115696", "11738", "", "451", "3891", "2294", "134070", 
"", "", "", "13772", "8038", "21810", "-8181", "13629", "120853", 
"81645", "8246", "30", "5977", "230380", "364450", "", "", "", 
"4304", "1051", "3733", "15352", "10297", "17302", "52039", "", 
"53467", "6471", "", "418", "32552", "2516", "95424", "147463", 
"", "1", "2356", "132206", "78752", "", "3672", "216987", "364450"
), V5 = c("2015-03", "", "", "", "108193", "17806", "125999", 
"11163", "", "256", "815", "3876", "142109", "", "", "", "10259", 
"4646", "14905", "-5766", "9139", "48488", "41933", "9680", "157", 
"3928", "113325", "255434", "", "", "", "1990", "667", "3368", 
"7035", "7914", "18698", "39672", "", "50603", "4493", "", "445", 
"11974", "2808", "70323", "109995", "", "1", "1563", "117142", 
"24842", "", "1891", "145439", "255434"), V6 = c("2014-03", "", 
"", "", "33045", "12029", "45074", "16006", "", "191", "226", 
"6336", "67833", "", "", "", "5947", "3214", "9161", "-3580", 
"5581", "20689", "11793", "3566", "66", "2021", "43716", "111549", 
"", "", "", "10364", "3308", "1972", "7021", "6496", "8223", 
"37384", "", "30711", "2136", "18", "428", "1079", "10455", "44827", 
"82211", "", "1", "2474", "27043", "1183", "", "-1363", "29338", 
"111549"), V7 = c("2013-03", "", "", "", "30396", "2919", "33315", 
"4952", "", "208", "186", "4501", "43162", "", "", "", "3882", 
"2612", "6494", "-2686", "3808", "1797", "11294", "2229", "52", 
"1444", "20624", "63786", "", "", "", "5448", "2012", "1112", 
"5318", "4929", "5176", "23995", "", "22462", "643", "60", "389", 
"537", "15724", "39815", "63810", "", "1", "1337", "21655", "-20491", 
"", "-2526", "-24", "63786"), V8 = c("2012-03", "", "", "", "16857", 
"5480", "22337", "1315", "", "97", "139", "4011", "27899", "", 
"", "", "3114", "1565", "4679", "-2216", "2463", "1890", "11436", 
"2056", "43", "1423", "19311", "47210", "", "", "", "1283", "537", 
"745", "2484", "4350", "2352", "11751", "", "", "413", "", "529", 
"2895", "134", "3971", "15722", "", "1", "1096", "20778", "12552", 
"", "-2939", "31488", "47210"), V9 = c("2011-12", "", "", "", 
"3433", "8219", "11652", "559", "", "", "51", "978", "13241", 
"", "", "", "756", "594", "1350", "-617", "733", "235", "455", 
"224", "", "408", "2054", "15295", "", "", "", "1286", "16", 
"472", "362", "3969", "854", "6961", "", "", "240", "", "454", 
"102", "120", "917", "7878", "", "0", "", "", "", "", "7417", 
"7418", "15295"), V10 = c("2010-12", "", "", "", "3254", "6803", 
"10057", "94", "", "", "294", "596", "11040", "", "", "", "696", 
"540", "1236", "-455", "781", "64", "368", "232", "101", "119", 
"1665", "12705", "", "", "", "93", "16", "105", "", "", "6171", 
"6385", "", "", "131", "", "333", "50", "54", "567", "6952", 
"", "0", "", "", "", "", "5753", "5753", "12705"), V11 = c("2009-12", 
"", "", "", "2749", "4518", "7266", "38", "", "", "309", "529", 
"8143", "", "", "169", "568", "327", "1064", "-281", "783", "26", 
"203", "166", "73", "63", "1314", "9457", "", "", "", "", "24", 
"35", "", "", "4038", "4097", "", "", "70", "", "204", "41", 
"68", "383", "4480", "", "0", "", "", "", "", "4977", "4977", 
"9457")), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", 
"V8", "V9", "V10", "V11"), class = "data.frame", row.names = c(NA, 
-56L)), Alphabet = structure(list(V1 = c("Fiscal year ends in December. USD in millions except per share data.", 
"Assets", "Current assets", "Cash", "Cash and cash equivalents", 
"Short-term investments", "Total cash", "Receivables", "Inventories", 
"Deferred income taxes", "Prepaid expenses", "Other current assets", 
"Total current assets", "Non-current assets", "Property, plant and equipment", 
"Gross property, plant and equipment", "Accumulated Depreciation", 
"Net property, plant and equipment", "Equity and other investments", 
"Goodwill", "Intangible assets", "Deferred income taxes", "Other long-term assets", 
"Total non-current assets", "Total assets", "Liabilities and stockholders' equity", 
"Liabilities", "Current liabilities", "Short-term debt", "Capital leases", 
"Accounts payable", "Taxes payable", "Accrued liabilities", "Deferred revenues", 
"Other current liabilities", "Total current liabilities", "Non-current liabilities", 
"Long-term debt", "Capital leases", "Deferred taxes liabilities", 
"Deferred revenues", "Other long-term liabilities", "Total non-current liabilities", 
"Total liabilities", "Stockholders' equity", "Common stock", 
"Additional paid-in capital", "Retained earnings", "Accumulated other comprehensive income", 
"Total stockholders' equity", "Total liabilities and stockholders' equity"
), V2 = c("2017-12", "", "", "", "10715", "91156", "101871", 
"18336", "749", "", "", "3352", "124308", "", "", "59647", "-17264", 
"42383", "7813", "16747", "2692", "680", "2672", "72987", "197295", 
"", "", "", "", "", "3137", "881", "5464", "1432", "13269", "24183", 
"", "3943", "26", "430", "340", "15871", "20610", "44793", "", 
"40247", "", "113247", "-992", "152502", "197295"), V3 = c("2016-12", 
"", "", "", "12918", "73415", "86333", "14137", "268", "", "4575", 
"95", "105408", "", "", "47527", "-13293", "34234", "5878", "16468", 
"3307", "383", "1819", "62089", "167497", "", "", "", "", "", 
"2041", "554", "4198", "1099", "8864", "16756", "", "3935", "", 
"226", "202", "7342", "11705", "28461", "", "36307", "", "105131", 
"-2402", "139036", "167497"), V4 = c("2015-12", "", "", "", "16549", 
"56517", "73066", "11556", "", "", "3139", "2353", "90114", "", 
"", "40146", "-11130", "29016", "5183", "15869", "3847", "251", 
"3181", "57347", "147461", "", "", "", "3000", "225", "1931", 
"302", "7097", "788", "5967", "19310", "", "1995", "", "189", 
"151", "5485", "7820", "27130", "", "32982", "", "89223", "-1874", 
"120331", "147461"), V5 = c("2014-12", "", "", "", "18347", "46048", 
"64395", "9383", "", "1322", "3412", "2173", "80685", "", "", 
"32746", "-8863", "23883", "3079", "15599", "4607", "", "3280", 
"50448", "131133", "", "", "", "1999", "10", "1715", "96", "6386", 
"752", "5847", "16805", "", "2992", "236", "1971", "104", "4525", 
"9828", "26633", "", "28767", "", "75706", "27", "104500", "131133"
), V6 = c("2013-12", "", "", "", "18898", "39819", "58717", "8882", 
"426", "1526", "2827", "508", "72886", "", "", "23837", "-7313", 
"16524", "1976", "11492", "6066", "", "1976", "38034", "110920", 
"", "", "", "3000", "9", "2453", "24", "5484", "1062", "3876", 
"15908", "", "1990", "246", "1947", "139", "3381", "7703", "23611", 
"", "25922", "", "61262", "125", "87309", "110920"), V7 = c("2012-12", 
"", "", "", "14778", "33310", "48088", "7885", "505", "1144", 
"2132", "700", "60454", "", "", "17697", "-5843", "11854", "1469", 
"10537", "7473", "", "2011", "33344", "93798", "", "", "", "2549", 
"", "2012", "240", "4729", "895", "3912", "14337", "", "2988", 
"", "1872", "100", "2786", "7746", "22083", "", "22835", "", 
"48342", "538", "71715", "93798"), V8 = c("2011-12", "", "", 
"", "9983", "34643", "44626", "5427", "", "215", "1745", "745", 
"52758", "", "", "14400", "-4797", "9603", "790", "7346", "1578", 
"", "499", "19816", "72574", "", "", "", "1218", "", "588", "197", 
"2538", "547", "3825", "8913", "", "2986", "", "287", "44", "2199", 
"5516", "14429", "", "20264", "", "37605", "276", "58145", "72574"
), V9 = c("2010-12", "", "", "", "13630", "21345", "34975", "4252", 
"", "259", "1326", "750", "41562", "", "", "11771", "-4012", 
"7759", "523", "6256", "1044", "265", "442", "16289", "57851", 
"", "", "", "3465", "", "483", "37", "3256", "394", "2361", "9996", 
"", "", "", "", "35", "1579", "1614", "11610", "", "18235", "", 
"27868", "138", "46241", "57851"), V10 = c("2009-12", "", "", 
"", "10198", "14287", "24485", "3178", "", "644", "836", "23", 
"29167", "", "", "8130", "-3286", "4845", "129", "4903", "775", 
"263", "416", "11330", "40497", "", "", "", "", "", "216", "", 
"1264", "285", "982", "2747", "", "", "", "", "42", "1703", "1745", 
"4493", "", "0", "15817", "20082", "105", "36004", "40497"), 
    V11 = c("2008-12", "", "", "", "8657", "7189", "15846", "2642", 
    "", "286", "1404", "", "20178", "", "", "7576", "-2342", 
    "5234", "85", "4840", "997", "", "434", "11589", "31768", 
    "", "", "", "", "", "178", "82", "1013", "218", "812", "2302", 
    "", "", "", "13", "30", "1184", "1227", "3529", "", "0", 
    "14450", "13562", "227", "28239", "31768")), .Names = c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11"), class = "data.frame", row.names = c(NA, 
-51L))), .Names = c("Alibaba", "Alphabet"))

到目前为止,我可以发布我所拥有的,但这不会让我有任何收获。

你可以在这里尝试Lappy on list names技巧。 不必将列表本身提供给lappy,您可以提供names向量并调用所需的函数。 在您的功能中,您首先从列表(BS和IS)中选择所需的数据,然后可以使用它。 为此,列表BS和IS中的名称必须相同。如果不是这种情况,则在函数中选择数据将失败(或者至少不会提供匹配的数据对)。 我认为如果可能的话,稍微整理一下列表B中的数据也会有帮助。现在colname和rowname存储在第1行和第1列中。这使得处理数据更加困难

    your_function = function(name)
    {
     data <- BS[[name]] # load data from list BS
     data2 <- IS[[name]] # load data from list IS

     # the following calculations make no sence. Just for illustration
     temp <- data[5, 2:NCOL(data)] # select desired variable-row
     temp <- matrix(as.numeric(temp), ncol = NCOL(temp)) # convert selection into numeric format
     temp2 <- data[6, 2:NCOL(data)] # select desired variable-row number 2
     temp2 <- matrix(as.numeric(temp2), ncol = NCOL(temp2)) # convert it into numeric as well
     temp <- temp/temp2 # perform desired operation with variable 1 and 2 
     rownames(temp) <- paste0(data[5,1], " / ", data[6,1]) # set rownames

     # insert more calculations here by selecting more desired rows
     temp3 <- matrix(as.numeric(data2[7, 3:NCOL(data2)]), ncol = NCOL(data2)-2) # lets just take row 7 from data2
     temp3 <- temp/temp3 # lets divide temp by the value from data2
     rownames(temp3) <- paste0("values from BS / ", data2[7,1])

     # add all calculated rows together
     temp <- rbind(temp, temp3)
     colnames(temp) <- data[1,2:NCOL(data)] # set colnames
     print(names(data))
     return(temp) # return the results 
    }

    test <- lapply(names(BS), your_function)
    names(test) = names(BS)
    test

请创建代表您的情况的较小的数据示例。请问较小的数据示例是什么意思?每个名单上都有两家公司。您是否建议我将每个公司的一些变量删除为2-3?谢谢!我真的很感激。我明白你关于进一步清理数据的观点。我刚刚在整个列表上运行了它,由于列表的大小不同,一些计算是不正确的。我会确保所有的清单都是相同的,这应该是完美的工作。再次感谢!
    your_function = function(name)
    {
     data <- BS[[name]] # load data from list BS
     data2 <- IS[[name]] # load data from list IS

     # the following calculations make no sence. Just for illustration
     temp <- data[5, 2:NCOL(data)] # select desired variable-row
     temp <- matrix(as.numeric(temp), ncol = NCOL(temp)) # convert selection into numeric format
     temp2 <- data[6, 2:NCOL(data)] # select desired variable-row number 2
     temp2 <- matrix(as.numeric(temp2), ncol = NCOL(temp2)) # convert it into numeric as well
     temp <- temp/temp2 # perform desired operation with variable 1 and 2 
     rownames(temp) <- paste0(data[5,1], " / ", data[6,1]) # set rownames

     # insert more calculations here by selecting more desired rows
     temp3 <- matrix(as.numeric(data2[7, 3:NCOL(data2)]), ncol = NCOL(data2)-2) # lets just take row 7 from data2
     temp3 <- temp/temp3 # lets divide temp by the value from data2
     rownames(temp3) <- paste0("values from BS / ", data2[7,1])

     # add all calculated rows together
     temp <- rbind(temp, temp3)
     colnames(temp) <- data[1,2:NCOL(data)] # set colnames
     print(names(data))
     return(temp) # return the results 
    }

    test <- lapply(names(BS), your_function)
    names(test) = names(BS)
    test
head(sort(table(BS$Alibaba$V1), decreasing = T))
# Accrued liabilities                  Deferred income taxes 
# 2                                      2 
# Deferred revenues                       Accounts payable 
# 2                                      1 
# Accumulated Depreciation Accumulated other comprehensive income 
# 1                                      1