2010-11-06 162 views
9

我有關於2008-9年丹麥議會所有選票的數據。問題是,每票被記錄在了自己的.txt文件格式如下(見後的附錄的三票來自R轉儲):合併R中的很多數據幀

Name    Party Vote 
Anders Samuelsen LA  For 
Rasmus Prehn  S  Imod 
...    ... ... 

我想合併所有文件用下面的格式一個數據幀:

Name    Party Vote1 Vote2 Vote3 
Anders Samuelsen LA  For Imod Imod 
Rasmus Prehn  S  Imod For Fraværende 

(丹麥語翻譯:對於= YES,IMOD =沒有,FravÃ|rende=無)

我總共有超過200個文件,所以問題是:我如何編寫一個循環,將所有數據幀合併到上面所需的結構中?

附錄:

Vote1:

structure(list(V1 = structure(c(2L, 3L, 4L, 7L, 8L, 5L, 6L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
36L, 37L, 38L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 
50L, 51L, 52L, 53L, 39L, 54L, 55L, 56L, 60L, 61L, 62L, 63L, 64L, 
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 57L, 58L, 59L, 
75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 
88L, 89L, 90L, 91L, 92L, 93L, 94L, 96L, 97L, 98L, 95L, 99L, 100L, 
101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 
112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 
123L, 125L, 126L, 127L, 124L, 128L, 129L, 130L, 131L, 132L, 133L, 
134L, 135L, 136L, 137L, 139L, 140L, 141L, 138L, 142L, 143L, 144L, 
145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 
160L, 161L, 162L, 163L, 164L, 156L, 157L, 158L, 159L, 165L, 166L, 
167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 
178L, 179L, 1L), .Label = c("Özlem Sara Cekic", "Anders Samuelsen", 
"Anita Christensen", "Anita Knakkergaard", "Anne-Marie Meldgaard", 
"Anne-Mette Winther Christians", "Anne Baastrup", "Anne Grete Holmsgaard", 
"Annette Lilja Vilhelmsen", "Benny Engelbrecht", "Bent Bøgsted", 
"Bente Dahl", "Bertel Haarder", "Birgitte Josefsen", "Birthe Rønn Hornbech", 
"Bjarne Laustsen", "Brian Mikkelsen", "Britta Schall Holberg", 
"Carina Christensen", "Carsten Hansen", "Charlotte Dyremose", 
"Christine Antorini", "Claus Hjort Frederiksen", "Colette L. Brix", 
"Connie Hedegaard", "Dennis Flydtkjær", "Edmund Joensen", "Eigil Andersen", 
"Ellen Trane Nørby", "Erling Bonnesen", "Eva Kjer Hansen", "Eyvind Vesselbo", 
"Flemming Bonne", "Flemming Damgaard Larsen", "Flemming Møller", 
"Flemming Møller Mortensen", "Frank Aaen", "Gitte Lillelund Bech", 
"Høgni Hoydal", "Hanne Agersnap", "Hans Christian Schmidt", 
"Hans Christian Thoning", "Hans Kristian Skibby", "Helge Adam Møller", 
"Helge Sander", "Helle Sjelle", "Helle Thorning-Schmidt", "Henriette Kjær", 
"Henrik Brodersen", "Henrik Dam Kristensen", "Henrik Høegh", 
"Henrik Sass Larsen", "Holger K. Nielsen", "Ib Poulsen", "Ida Auken", 
"Inger Støjberg", "Jørgen Poulsen", "Jørgen S. Lundsgaard", 
"Jørn Dohrmann", "Jacob Jensen", "Jakob Axel Nielsen", "Jens Christian Lund", 
"Jens Kirk", "Jens Peter Vernersen", "Jens Vibjerg", "Jeppe Kofod", 
"Jesper Langballe", "Jesper Petersen", "Johanne Schmidt-Nielsen", 
"John Dyrby Paulsen", "Johs. Poulsen", "Jonas Dahl", "Juliane Henningsen", 
"Julie Rademacher", "Kamal Qureshi", "Karen Ellemann", "Karen Hækkerup", 
"Karen J. Klint", "Karen Jespersen", "Karin Nødgaard", "Karina Lorentzen Dehnhardt", 
"Karl H. Bornhøft", "Karsten Lauritzen", "Karsten Nonbo", "Kim Andersen", 
"Kim Christiansen", "Kim Mortensen", "Kirsten Brosbøl", "Klaus Hækkerup", 
"Knud Kristensen", "Kristen Touborg", "Kristian Jensen", "Kristian Pihl Lorentzen", 
"Kristian Thulesen Dahl", "Lars-Emil Johansen", "Lars Barfoed", 
"Lars Christian Lilleholt", "Lars Løkke Rasmussen", "Leif Lahn Jensen", 
"Lene Espersen", "Lene Hansen", "Lennart Damsbo-Andersen", "Line Barfod", 
"Lise von Seelen", "Liselott Blixt", "Lone Dybkjær", "Lone Møller", 
"Louise Schack Elholm", "Magnus Heunicke", "Malou Aamund", "Margrethe Vestager", 
"Marianne Jelved", "Marion Pedersen", "Marlene Harpsøe", "Martin Henriksen", 
"Meta Fuglsang", "Mette Frederiksen", "Mette Gjerskov", "Michael Aastrup Jensen", 
"Mike Legarth", "Mikkel Dencker", "Mogens Jensen", "Mogens Lykketoft", 
"Morten Østergaard", "Morten Bødskov", "Morten Helveg Petersen", 
"Morten Messerschmidt", "Nanna Westerby", "Naser Khader", "Nick Hækkerup", 
"Niels Christian Nielsen", "Niels Helveg Petersen", "Niels Sindal", 
"Ole Hækkerup", "Ole Sohn", "Ole Vagn Christensen", "Orla Hav", 
"Per Ørum Jørgensen", "Per Bisgaard", "Per Clausen", "Per Stig Møller", 
"Pernille Frahm", "Pernille Vigsø Bagge", "Peter Christensen", 
"Peter Juel Jensen", "Peter Skaarup", "Pia Adelsteen", "Pia Christmas-Møller", 
"Pia Kjærsgaard", "Pia Olsen Dyhr", "Poul Andersen", "Preben Rudiengaard", 
"Rasmus Prehn", "René Christensen", "René Skau Björnsson", 
"Søren Espersen", "Søren Gade", "Søren Krarup", "Søren Pind", 
"Simon Emil Ammitzbøll", "Sophie Hæstorp Andersen", "Sophie Løhde", 
"Steen Gade", "Svend Auken", "Thomas Adelskov", "Thomas Jensen", 
"Thor Pedersen", "Tina Nedergaard", "Tina Petersen", "Tom Behnke", 
"Torben Hansen", "Torsten Schack Pedersen", "Troels Christensen", 
"Troels Lund Poulsen", "Ulla Tørnæs", "Villum Christensen", 
"Villy Søvndal", "Vivi Kier", "Yildiz Akdogan"), class = "factor"), 
    V2 = structure(c(5L, 1L, 1L, 8L, 8L, 7L, 13L, 8L, 7L, 1L, 
    6L, 13L, 13L, 13L, 7L, 4L, 13L, 4L, 7L, 4L, 7L, 13L, 1L, 
    4L, 1L, 10L, 8L, 13L, 13L, 13L, 13L, 8L, 13L, 13L, 7L, 2L, 
    13L, 8L, 13L, 13L, 1L, 4L, 13L, 4L, 7L, 4L, 1L, 7L, 13L, 
    7L, 8L, 11L, 1L, 8L, 13L, 13L, 4L, 7L, 13L, 7L, 13L, 7L, 
    1L, 8L, 2L, 7L, 6L, 8L, 3L, 7L, 6L, 4L, 1L, 8L, 13L, 7L, 
    7L, 13L, 1L, 8L, 8L, 13L, 13L, 13L, 1L, 7L, 7L, 7L, 4L, 8L, 
    13L, 13L, 1L, 4L, 13L, 13L, 9L, 7L, 4L, 7L, 7L, 2L, 7L, 1L, 
    6L, 7L, 13L, 7L, 13L, 6L, 6L, 13L, 1L, 1L, 8L, 7L, 7L, 13L, 
    4L, 1L, 7L, 7L, 7L, 6L, 1L, 6L, 8L, 4L, 7L, 7L, 6L, 7L, 7L, 
    8L, 7L, 7L, 13L, 2L, 4L, 4L, 8L, 8L, 13L, 13L, 1L, 1L, 12L, 
    1L, 8L, 7L, 13L, 7L, 1L, 7L, 12L, 7L, 13L, 8L, 7L, 1L, 13L, 
    1L, 13L, 7L, 7L, 13L, 13L, 1L, 4L, 7L, 13L, 13L, 13L, 13L, 
    5L, 8L, 4L, 7L, 8L), .Label = c("DF", "EL", "IA", "KF", "LA", 
    "RV", "S", "SF", "SIU", "SP", "TF", "UFG", "V"), class = "factor"), 
    V3 = structure(c(1L, 1L, 1L, 3L, 3L, 2L, 1L, 3L, 3L, 1L, 
    3L, 1L, 1L, 2L, 3L, 1L, 1L, 2L, 2L, 1L, 3L, 2L, 2L, 2L, 1L, 
    2L, 3L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 
    1L, 1L, 2L, 2L, 2L, 1L, 1L, 3L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 
    1L, 1L, 3L, 1L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 
    2L, 1L, 2L, 2L, 2L, 3L, 2L, 1L, 1L, 3L, 3L, 2L, 1L, 1L, 2L, 
    3L, 2L, 3L, 2L, 3L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 3L, 2L, 3L, 
    3L, 3L, 3L, 1L, 3L, 3L, 1L, 3L, 2L, 2L, 3L, 1L, 1L, 1L, 3L, 
    3L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 3L, 3L, 
    3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 1L, 2L, 
    1L, 2L, 2L, 2L, 3L, 1L, 3L, 1L, 2L, 1L, 3L, 1L, 2L, 2L, 2L, 
    2L, 1L, 1L, 2L, 3L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 
    2L, 1L, 3L, 2L), .Label = c("For", "Fraværende", "Imod"), class = "factor")), .Names = c("V1", 
"V2", "V3"), class = "data.frame", row.names = c(NA, -179L)) 

Vote2:

structure(list(V1 = structure(c(2L, 3L, 4L, 7L, 8L, 5L, 6L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
36L, 37L, 38L, 39L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 
50L, 51L, 52L, 53L, 54L, 40L, 55L, 56L, 59L, 60L, 61L, 62L, 63L, 
64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 57L, 58L, 74L, 
75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 
88L, 89L, 90L, 91L, 92L, 93L, 95L, 96L, 97L, 94L, 98L, 99L, 100L, 
101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 
112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 
124L, 125L, 126L, 123L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 
134L, 135L, 137L, 138L, 139L, 140L, 136L, 141L, 142L, 143L, 144L, 
145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 159L, 
160L, 161L, 162L, 163L, 155L, 156L, 157L, 158L, 164L, 165L, 166L, 
167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 
178L, 179L, 1L), .Label = c("Özlem Sara Cekic", "Anders Samuelsen", 
"Anita Christensen", "Anita Knakkergaard", "Anne-Marie Meldgaard", 
"Anne-Mette Winther Christians", "Anne Baastrup", "Anne Grete Holmsgaard", 
"Annette Lilja Vilhelmsen", "Bendt Bendtsen", "Benny Engelbrecht", 
"Bent Bøgsted", "Bente Dahl", "Bertel Haarder", "Birgitte Josefsen", 
"Birthe Rønn Hornbech", "Bjarne Laustsen", "Brian Mikkelsen", 
"Britta Schall Holberg", "Carina Christensen", "Carsten Hansen", 
"Charlotte Dyremose", "Christine Antorini", "Claus Hjort Frederiksen", 
"Colette L. Brix", "Connie Hedegaard", "Dennis Flydtkjær", "Edmund Joensen", 
"Eigil Andersen", "Ellen Trane Nørby", "Erling Bonnesen", "Eva Kjer Hansen", 
"Eyvind Vesselbo", "Flemming Bonne", "Flemming Damgaard Larsen", 
"Flemming Møller", "Flemming Møller Mortensen", "Frank Aaen", 
"Gitte Lillelund Bech", "Høgni Hoydal", "Hanne Agersnap", "Hans Christian Schmidt", 
"Hans Christian Thoning", "Hans Kristian Skibby", "Helge Adam Møller", 
"Helge Sander", "Helle Sjelle", "Helle Thorning-Schmidt", "Henriette Kjær", 
"Henrik Brodersen", "Henrik Dam Kristensen", "Henrik Høegh", 
"Henrik Sass Larsen", "Holger K. Nielsen", "Ib Poulsen", "Inger Støjberg", 
"Jørgen Poulsen", "Jørn Dohrmann", "Jacob Jensen", "Jakob Axel Nielsen", 
"Jens Christian Lund", "Jens Kirk", "Jens Peter Vernersen", "Jens Vibjerg", 
"Jeppe Kofod", "Jesper Langballe", "Jesper Petersen", "Johanne Schmidt-Nielsen", 
"John Dyrby Paulsen", "Johs. Poulsen", "Jonas Dahl", "Juliane Henningsen", 
"Julie Rademacher", "Kamal Qureshi", "Karen Ellemann", "Karen Hækkerup", 
"Karen J. Klint", "Karen Jespersen", "Karin Nødgaard", "Karina Lorentzen Dehnhardt", 
"Karl H. Bornhøft", "Karsten Lauritzen", "Karsten Nonbo", "Kim Andersen", 
"Kim Christiansen", "Kim Mortensen", "Kirsten Brosbøl", "Klaus Hækkerup", 
"Knud Kristensen", "Kristen Touborg", "Kristian Jensen", "Kristian Pihl Lorentzen", 
"Kristian Thulesen Dahl", "Lars-Emil Johansen", "Lars Barfoed", 
"Lars Christian Lilleholt", "Lars Løkke Rasmussen", "Leif Lahn Jensen", 
"Lene Espersen", "Lene Hansen", "Lennart Damsbo-Andersen", "Line Barfod", 
"Lise von Seelen", "Liselott Blixt", "Lone Dybkjær", "Lone Møller", 
"Louise Schack Elholm", "Magnus Heunicke", "Malou Aamund", "Margrethe Vestager", 
"Marianne Jelved", "Marion Pedersen", "Marlene Harpsøe", "Martin Henriksen", 
"Meta Fuglsang", "Mette Frederiksen", "Mette Gjerskov", "Michael Aastrup Jensen", 
"Mike Legarth", "Mikkel Dencker", "Mogens Jensen", "Mogens Lykketoft", 
"Morten Østergaard", "Morten Bødskov", "Morten Helveg Petersen", 
"Morten Messerschmidt", "Nanna Westerby", "Naser Khader", "Nick Hækkerup", 
"Niels Christian Nielsen", "Niels Helveg Petersen", "Niels Sindal", 
"Ole Hækkerup", "Ole Sohn", "Ole Vagn Christensen", "Per Ørum Jørgensen", 
"Per Bisgaard", "Per Clausen", "Per Husted", "Per Stig Møller", 
"Pernille Frahm", "Pernille Vigsø Bagge", "Peter Christensen", 
"Peter Juel Jensen", "Peter Skaarup", "Pia Adelsteen", "Pia Christmas-Møller", 
"Pia Kjærsgaard", "Pia Olsen Dyhr", "Poul Andersen", "Preben Rudiengaard", 
"Rasmus Prehn", "René Christensen", "René Skau Björnsson", 
"Søren Espersen", "Søren Gade", "Søren Krarup", "Søren Pind", 
"Simon Emil Ammitzbøll", "Sophie Hæstorp Andersen", "Sophie Løhde", 
"Steen Gade", "Svend Auken", "Thomas Adelskov", "Thomas Jensen", 
"Thor Pedersen", "Tina Nedergaard", "Tina Petersen", "Tom Behnke", 
"Torben Hansen", "Torsten Schack Pedersen", "Trine Mach", "Troels Christensen", 
"Troels Lund Poulsen", "Ulla Tørnæs", "Villum Christensen", 
"Villy Søvndal", "Vivi Kier", "Yildiz Akdogan"), class = "factor"), 
    V2 = structure(c(5L, 1L, 1L, 8L, 8L, 7L, 13L, 8L, 4L, 7L, 
    1L, 6L, 13L, 13L, 13L, 7L, 4L, 13L, 4L, 7L, 4L, 7L, 13L, 
    1L, 4L, 1L, 10L, 8L, 13L, 13L, 13L, 13L, 8L, 13L, 13L, 7L, 
    2L, 13L, 8L, 13L, 13L, 1L, 4L, 13L, 4L, 7L, 4L, 1L, 7L, 13L, 
    7L, 8L, 11L, 1L, 13L, 13L, 4L, 7L, 13L, 7L, 13L, 7L, 1L, 
    8L, 2L, 7L, 6L, 8L, 3L, 7L, 6L, 1L, 8L, 13L, 7L, 7L, 13L, 
    1L, 8L, 8L, 13L, 13L, 13L, 1L, 7L, 7L, 7L, 4L, 8L, 13L, 13L, 
    1L, 4L, 13L, 13L, 9L, 7L, 4L, 7L, 7L, 2L, 7L, 1L, 6L, 7L, 
    13L, 7L, 13L, 6L, 6L, 13L, 1L, 1L, 8L, 7L, 7L, 13L, 4L, 1L, 
    7L, 7L, 7L, 6L, 1L, 6L, 8L, 4L, 7L, 7L, 6L, 7L, 7L, 8L, 7L, 
    13L, 2L, 7L, 4L, 4L, 8L, 8L, 13L, 13L, 1L, 1L, 12L, 1L, 8L, 
    7L, 13L, 7L, 1L, 7L, 12L, 7L, 13L, 8L, 7L, 1L, 13L, 1L, 13L, 
    7L, 7L, 13L, 13L, 1L, 4L, 7L, 13L, 8L, 13L, 13L, 13L, 5L, 
    8L, 4L, 7L, 8L), .Label = c("DF", "EL", "IA", "KF", "LA", 
    "RV", "S", "SF", "SIU", "SP", "TF", "UFG", "V"), class = "factor"), 
    V3 = structure(c(3L, 1L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 
    1L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 
    1L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 
    3L, 1L, 3L, 3L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 3L, 2L, 1L, 2L, 
    3L, 3L, 3L, 3L, 3L, 2L, 3L, 1L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 
    2L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 1L, 2L, 
    3L, 3L, 2L, 2L, 3L, 3L, 1L, 3L, 3L, 2L, 2L, 3L, 2L, 3L, 2L, 
    2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 1L, 3L, 2L, 
    2L, 2L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 
    2L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 1L, 
    2L, 2L, 3L, 2L, 3L, 3L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 
    1L, 2L, 3L, 3L, 3L, 2L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 
    2L, 3L, 3L, 2L), .Label = c("For", "Fraværende", "Imod"), class = "factor")), .Names = c("V1", 
"V2", "V3"), class = "data.frame", row.names = c(NA, -179L)) 

Vote3:

structure(list(V1 = structure(c(2L, 3L, 4L, 7L, 8L, 5L, 6L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
36L, 37L, 38L, 39L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 
50L, 51L, 52L, 53L, 54L, 40L, 55L, 56L, 59L, 60L, 61L, 62L, 63L, 
64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 57L, 58L, 74L, 
75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 
88L, 89L, 90L, 91L, 92L, 93L, 95L, 96L, 97L, 94L, 98L, 99L, 100L, 
101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 
112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 
124L, 125L, 126L, 123L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 
134L, 135L, 137L, 138L, 139L, 140L, 136L, 141L, 142L, 143L, 144L, 
145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 159L, 
160L, 161L, 162L, 163L, 155L, 156L, 157L, 158L, 164L, 165L, 166L, 
167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 
178L, 179L, 1L), .Label = c("Özlem Sara Cekic", "Anders Samuelsen", 
"Anita Christensen", "Anita Knakkergaard", "Anne-Marie Meldgaard", 
"Anne-Mette Winther Christians", "Anne Baastrup", "Anne Grete Holmsgaard", 
"Annette Lilja Vilhelmsen", "Bendt Bendtsen", "Benny Engelbrecht", 
"Bent Bøgsted", "Bente Dahl", "Bertel Haarder", "Birgitte Josefsen", 
"Birthe Rønn Hornbech", "Bjarne Laustsen", "Brian Mikkelsen", 
"Britta Schall Holberg", "Carina Christensen", "Carsten Hansen", 
"Charlotte Dyremose", "Christine Antorini", "Claus Hjort Frederiksen", 
"Colette L. Brix", "Connie Hedegaard", "Dennis Flydtkjær", "Edmund Joensen", 
"Eigil Andersen", "Ellen Trane Nørby", "Erling Bonnesen", "Eva Kjer Hansen", 
"Eyvind Vesselbo", "Flemming Bonne", "Flemming Damgaard Larsen", 
"Flemming Møller", "Flemming Møller Mortensen", "Frank Aaen", 
"Gitte Lillelund Bech", "Høgni Hoydal", "Hanne Agersnap", "Hans Christian Schmidt", 
"Hans Christian Thoning", "Hans Kristian Skibby", "Helge Adam Møller", 
"Helge Sander", "Helle Sjelle", "Helle Thorning-Schmidt", "Henriette Kjær", 
"Henrik Brodersen", "Henrik Dam Kristensen", "Henrik Høegh", 
"Henrik Sass Larsen", "Holger K. Nielsen", "Ib Poulsen", "Inger Støjberg", 
"Jørgen Poulsen", "Jørn Dohrmann", "Jacob Jensen", "Jakob Axel Nielsen", 
"Jens Christian Lund", "Jens Kirk", "Jens Peter Vernersen", "Jens Vibjerg", 
"Jeppe Kofod", "Jesper Langballe", "Jesper Petersen", "Johanne Schmidt-Nielsen", 
"John Dyrby Paulsen", "Johs. Poulsen", "Jonas Dahl", "Juliane Henningsen", 
"Julie Rademacher", "Kamal Qureshi", "Karen Ellemann", "Karen Hækkerup", 
"Karen J. Klint", "Karen Jespersen", "Karin Nødgaard", "Karina Lorentzen Dehnhardt", 
"Karl H. Bornhøft", "Karsten Lauritzen", "Karsten Nonbo", "Kim Andersen", 
"Kim Christiansen", "Kim Mortensen", "Kirsten Brosbøl", "Klaus Hækkerup", 
"Knud Kristensen", "Kristen Touborg", "Kristian Jensen", "Kristian Pihl Lorentzen", 
"Kristian Thulesen Dahl", "Lars-Emil Johansen", "Lars Barfoed", 
"Lars Christian Lilleholt", "Lars Løkke Rasmussen", "Leif Lahn Jensen", 
"Lene Espersen", "Lene Hansen", "Lennart Damsbo-Andersen", "Line Barfod", 
"Lise von Seelen", "Liselott Blixt", "Lone Dybkjær", "Lone Møller", 
"Louise Schack Elholm", "Magnus Heunicke", "Malou Aamund", "Margrethe Vestager", 
"Marianne Jelved", "Marion Pedersen", "Marlene Harpsøe", "Martin Henriksen", 
"Meta Fuglsang", "Mette Frederiksen", "Mette Gjerskov", "Michael Aastrup Jensen", 
"Mike Legarth", "Mikkel Dencker", "Mogens Jensen", "Mogens Lykketoft", 
"Morten Østergaard", "Morten Bødskov", "Morten Helveg Petersen", 
"Morten Messerschmidt", "Nanna Westerby", "Naser Khader", "Nick Hækkerup", 
"Niels Christian Nielsen", "Niels Helveg Petersen", "Niels Sindal", 
"Ole Hækkerup", "Ole Sohn", "Ole Vagn Christensen", "Per Ørum Jørgensen", 
"Per Bisgaard", "Per Clausen", "Per Husted", "Per Stig Møller", 
"Pernille Frahm", "Pernille Vigsø Bagge", "Peter Christensen", 
"Peter Juel Jensen", "Peter Skaarup", "Pia Adelsteen", "Pia Christmas-Møller", 
"Pia Kjærsgaard", "Pia Olsen Dyhr", "Poul Andersen", "Preben Rudiengaard", 
"Rasmus Prehn", "René Christensen", "René Skau Björnsson", 
"Søren Espersen", "Søren Gade", "Søren Krarup", "Søren Pind", 
"Simon Emil Ammitzbøll", "Sophie Hæstorp Andersen", "Sophie Løhde", 
"Steen Gade", "Svend Auken", "Thomas Adelskov", "Thomas Jensen", 
"Thor Pedersen", "Tina Nedergaard", "Tina Petersen", "Tom Behnke", 
"Torben Hansen", "Torsten Schack Pedersen", "Trine Mach", "Troels Christensen", 
"Troels Lund Poulsen", "Ulla Tørnæs", "Villum Christensen", 
"Villy Søvndal", "Vivi Kier", "Yildiz Akdogan"), class = "factor"), 
    V2 = structure(c(5L, 1L, 1L, 8L, 8L, 7L, 13L, 8L, 4L, 7L, 
    1L, 6L, 13L, 13L, 13L, 7L, 4L, 13L, 4L, 7L, 4L, 7L, 13L, 
    1L, 4L, 1L, 10L, 8L, 13L, 13L, 13L, 13L, 8L, 13L, 13L, 7L, 
    2L, 13L, 8L, 13L, 13L, 1L, 4L, 13L, 4L, 7L, 4L, 1L, 7L, 13L, 
    7L, 8L, 11L, 1L, 13L, 13L, 4L, 7L, 13L, 7L, 13L, 7L, 1L, 
    8L, 2L, 7L, 6L, 8L, 3L, 7L, 6L, 1L, 8L, 13L, 7L, 7L, 13L, 
    1L, 8L, 8L, 13L, 13L, 13L, 1L, 7L, 7L, 7L, 4L, 8L, 13L, 13L, 
    1L, 4L, 13L, 13L, 9L, 7L, 4L, 7L, 7L, 2L, 7L, 1L, 6L, 7L, 
    13L, 7L, 13L, 6L, 6L, 13L, 1L, 1L, 8L, 7L, 7L, 13L, 4L, 1L, 
    7L, 7L, 7L, 6L, 1L, 6L, 8L, 4L, 7L, 7L, 6L, 7L, 7L, 8L, 7L, 
    13L, 2L, 7L, 4L, 4L, 8L, 8L, 13L, 13L, 1L, 1L, 12L, 1L, 8L, 
    7L, 13L, 7L, 1L, 7L, 12L, 7L, 13L, 8L, 7L, 1L, 13L, 1L, 13L, 
    7L, 7L, 13L, 13L, 1L, 4L, 7L, 13L, 8L, 13L, 13L, 13L, 5L, 
    8L, 4L, 7L, 8L), .Label = c("DF", "EL", "IA", "KF", "LA", 
    "RV", "S", "SF", "SIU", "SP", "TF", "UFG", "V"), class = "factor"), 
    V3 = structure(c(3L, 3L, 3L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 
    3L, 2L, 3L, 3L, 2L, 1L, 3L, 3L, 3L, 2L, 2L, 1L, 2L, 2L, 2L, 
    3L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 2L, 2L, 
    3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 
    3L, 2L, 1L, 3L, 1L, 2L, 1L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 1L, 
    2L, 3L, 3L, 2L, 1L, 2L, 3L, 2L, 1L, 2L, 3L, 2L, 3L, 3L, 2L, 
    1L, 1L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 1L, 2L, 1L, 2L, 
    2L, 2L, 2L, 2L, 1L, 3L, 1L, 3L, 1L, 2L, 2L, 3L, 3L, 3L, 2L, 
    1L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 3L, 3L, 1L, 2L, 1L, 
    2L, 1L, 2L, 2L, 2L, 3L, 1L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 
    2L, 2L, 3L, 2L, 3L, 1L, 3L, 2L, 2L, 1L, 3L, 3L, 2L, 2L, 2L, 
    3L, 2L, 1L, 1L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 
    2L, 3L, 1L, 2L), .Label = c("For", "Fraværende", "Imod"), class = "factor")), .Names = c("V1", 
"V2", "V3"), class = "data.frame", row.names = c(NA, -179L)) 
+0

大家好,丹麥大人,你能否給我們提供文件本身所用的術語? – 2010-11-06 21:24:42

回答

10

這裏是一個稍微不同的plyr使用方法和哈德利重塑包。

library(plyr); 
library(reshape); 

# function to read a single file 

readFile = function(i){ 

    .file = paste("Vote", i, ".csv", sep = ""); 
    .vote = read.csv(.file); 
    names(.vote) = c('name', 'party', 'vote'); 
    .vote$id = paste("Vote", i, sep = ""); 
    return(.vote); 
} 

nFiles = 200; 

# read all data files and store as data frame 
vote = ldply(1:nFiles, readFile); 

# reshape data to get to the required format 
vote2 = cast(vote, name + party ~ id, value = 'vote') 
3

調整read.csv它是什麼類型的文件,你正在拉入這個例子假定文件的命名如下:

Vote1.csv Vote2.csv ... Vote200.csv

n <- 200 ## Number of votes 
df <- read.csv(file=paste("Vote1.csv",sep="")) 
df.tmp <- data.frame() ## initialize 

for (x in 2:n) { 
df.tmp <- read.csv(file=paste("Vote",n,".csv",sep="")) 
df <- merge(df,df.tmp[c(1,3)], by="V1") 
} 

colnames(df)[3:n] <- paste("Vote",1:n,sep="") 
colnames(df)[1:2] <- c("Name","Party") 
+1

成長中的對象:R地獄現在在你的圈子裏算你 – baptiste 2012-03-29 05:23:02

+0

我絕對有罪。 – 2012-03-29 05:25:27

5

這是我如何處理多個導入。我希望我的例子適合你的需求。 你要做的是讀取目錄中的所有文件名,將每個文件作爲列表元素和整個列表導入到一個data.frame中。我假設你的文件以voting_results_*開頭。

# Create a list of files 
import.files <- as.list(grep("voting_results_", list.files(), value = TRUE)) 

importResults <- function(x) { 
    read.table(x, header = TRUE) # customize this to meet your needs 
} 

# Using lapply on each list element, import all files into one list. 
voting.results <- lapply(import.files, FUN = importResults) 

# R bind all list elements into one big data.frame. 
voting.results <- do.call("rbind", voting.results) 

# Dance. 
3

如果您認爲黨始終是相同的,那麼就合併(兩次),由名稱(== 「V1」 在您的結構):

mergevotes <- merge(merge(Vote1, Vote2[, c(1,3)], by="V1"), Vote3[, c(1,3)], by="V1") 
names(mergevotes) <- c("Name", "Party", "Vote1", "Vote2", "Vote3") 

STR(mergevotes) data.frame':176 obs。 5個變量:
$名稱:因子瓦特/ 179水平「Ã-zlem Sara Cekic」,..:1 2 3 4 7 8 5 6 9 10 ...
$ Party:因子w/13水平「DF 「,」EL「,」IA「,..:8 5 1 1 8 8 7 13 8 7 ...
$投票1:因子w/3級別」For「,」FravÃ|rende「,..:2 1 1 1 3 3 2 1 3 3 ...
$投票2:因子w/3級別「For」,「FravÃ|rende」,..:2 3 1 1 3 2 2 2 2 3 ...
$ Vote3:因子w/3等級「For」,「FravÃ|rende」,..:2 3 3 2 2 1 1 2 2 1 ...
頭(mergevotes)

    Name Party  Vote1  Vote2  Vote3 
1  Özlem Sara Cekic SF Fraværende Fraværende Fraværende 
2  Anders Samuelsen LA   For  Imod  Imod 
3  Anita Christensen DF   For   For  Imod 
4 Anita Knakkergaard DF   For   For  Imod 
5   Anne Baastrup SF  Imod  Imod Fraværende 
6 Anne Grete Holmsgaard SF  Imod Fraværende Fraværende 

祝SO接口使它容易指定非比例字體例如Courier。它似乎在吃掉R提供的間距,以便在粘貼到文本部分時排列表格輸出。