第一行作为数据框列表中的列名

时间:2018-09-18 11:10:09

标签: r

我正在尝试使用lapply将数据帧列表的第一行设置为列名。

数据示例如下:

       V3      V4      V5      V6      V7      V8
1 2017-03 2016-03 2015-03 2014-03 2013-03 2012-03
2                                                
3                                                
4                                                
5  143736  106818  108193   33045   30396   16857
6    7065    8878   17806   12029    2919    5480

所以我试图将日期变成列名,以替换V3V4等。

数据:

 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)), Amazon = 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", "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", "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", 
    "Accrued liabilities", "Deferred revenues", "Total current liabilities", 
    "Non-current liabilities", "Long-term debt", "Capital leases", 
    "Deferred taxes liabilities", "Other long-term liabilities", 
    "Total non-current liabilities", "Total liabilities", "Stockholders' equity", 
    "Common stock", "Additional paid-in capital", "Retained earnings", 
    "Treasury stock", "Accumulated other comprehensive income", "Total stockholders' equity", 
    "Total liabilities and stockholders' equity"), V2 = c("2017-12", 
    "", "", "", "20522", "10464", "30986", "13164", "16047", "", 
    "60197", "", "", "23718", "38387", "6468", "68573", "-19707", 
    "48866", "13350", "3371", "", "5526", "71113", "131310", "", 
    "", "", "", "34616", "18170", "5097", "57883", "", "24743", "13183", 
    "1994", "5798", "45718", "103601", "", "5", "21389", "8636", 
    "-1837", "-484", "27709", "131310"), V3 = c("2016-12", "", "", 
    "", "19334", "6647", "25981", "8339", "11461", "", "45781", "", 
    "", "13998", "25989", "2454", "42441", "-13327", "29114", "3784", 
    "854", "", "3869", "37621", "83402", "", "", "", "", "25309", 
    "13739", "4768", "43816", "", "7694", "7519", "1787", "3301", 
    "20301", "64117", "", "5", "17186", "4916", "-1837", "-985", 
    "19285", "83402"), V4 = c("2015-12", "", "", "", "15890", "3918", 
    "19808", "6423", "10243", "", "36474", "", "", "9770", "18417", 
    "1866", "30053", "-8215", "21838", "3759", "762", "", "2611", 
    "28970", "65444", "", "", "", "", "20397", "10384", "3118", "33899", 
    "", "8235", "5948", "2016", "1962", "18161", "52060", "", "5", 
    "13394", "2545", "-1837", "-723", "13384", "65444"), V5 = c("2014-12", 
    "", "", "", "14557", "2859", "17416", "5612", "8299", "", "31327", 
    "", "", "7150", "14213", "1367", "22730", "-5763", "16967", "3319", 
    "764", "", "2128", "23178", "54505", "", "", "", "", "16459", 
    "9807", "1823", "28089", "", "8265", "4224", "1531", "1655", 
    "15675", "43764", "", "5", "11135", "1949", "-1837", "-511", 
    "10741", "54505"), V6 = c("2013-12", "", "", "", "8658", "3789", 
    "12447", "4767", "7411", "", "24625", "", "", "4584", "9274", 
    "951", "14809", "-3860", "10949", "2655", "645", "", "1285", 
    "15534", "40159", "", "", "", "", "15133", "6688", "1159", "22980", 
    "", "3191", "1990", "1028", "1224", "7433", "30413", "", "5", 
    "9573", "2190", "-1837", "-185", "9746", "40159"), V7 = c("2012-12", 
    "", "", "", "8084", "3364", "11448", "3364", "6031", "453", "21296", 
    "", "", "2966", "6228", "388", "9582", "-2522", "7060", "2552", 
    "725", "123", "799", "11259", "32555", "", "", "", "", "13318", 
    "5684", "", "19002", "", "3084", "746", "336", "1195", "5361", 
    "24363", "", "5", "8347", "1916", "-1837", "-239", "8192", "32555"
    ), V8 = c("2011-12", "", "", "", "5269", "4307", "9576", "2571", 
    "4992", "351", "17490", "", "", "", "3216", "2570", "5786", "-1369", 
    "4417", "1955", "647", "28", "741", "7788", "25278", "", "", 
    "", "", "11145", "3751", "", "14896", "", "255", "1160", "", 
    "1210", "2625", "17521", "", "5", "6990", "1955", "-877", "-316", 
    "7757", "25278"), V9 = c("2010-12", "", "", "", "3777", "4985", 
    "8762", "1587", "3202", "196", "13747", "", "", "", "2505", "751", 
    "3256", "-842", "2414", "1349", "563", "22", "702", "5050", "18797", 
    "", "", "", "", "8051", "2321", "", "10372", "", "184", "457", 
    "", "920", "1561", "11933", "", "5", "6325", "1324", "-600", 
    "-190", "6864", "18797"), V10 = c("2009-12", "", "", "", "3444", 
    "2922", "6366", "988", "2171", "272", "9797", "", "", "", "1500", 
    "415", "1915", "-625", "1290", "1234", "567", "18", "907", "4016", 
    "13813", "", "", "", "", "5605", "1759", "", "7364", "", "109", 
    "143", "", "940", "1192", "8556", "", "5", "5736", "172", "-600", 
    "-56", "5257", "13813"), V11 = c("2008-12", "", "", "", "2769", 
    "958", "3727", "827", "1399", "204", "6157", "", "", "", "1243", 
    "166", "1409", "-555", "854", "438", "160", "145", "560", "2157", 
    "8314", "", "", "", "59", "3594", "1093", "", "4746", "", "409", 
    "124", "", "363", "896", "5642", "", "4", "4121", "-730", "-600", 
    "-123", "2672", "8314")), .Names = c("V1", "V2", "V3", "V4", 
    "V5", "V6", "V7", "V8", "V9", "V10", "V11"), class = "data.frame", row.names = c(NA, 
    -48L))), .Names = c("Alibaba", "Alphabet", "Amazon"))

1 个答案:

答案 0 :(得分:1)

这应该有效:

dflist <- structure(...) # your dput

dflist1 <- lapply(dflist, function(x){
  names(x) <- x[1,]
  x <- x[-1,]
  return(x)
})

第一列有所不同,因此要排除此列,请使用以下修改:

dflist1 <- lapply(dflist, function(x){
  names(x)[-1] <- x[1,-1]
  x <- x[-1,]
  return(x)
})

> head(dflist1[[1]])
                         V1 2018-03 2017-03 2016-03 2015-03 2014-03 2013-03 2012-03 2011-12 2010-12 2009-12
2                    Assets                                                                                
3            Current assets                                                                                
4                      Cash                                                                                
5 Cash and cash equivalents  199309  143736  106818  108193   33045   30396   16857    3433    3254    2749
6    Short-term investments   10901    7065    8878   17806   12029    2919    5480    8219    6803    4518
7                Total cash  210210  150801  115696  125999   45074   33315   22337   11652   10057    7266