Dear all, I have a data.frame like the sample below, and I would like to expand my data.frame using "population" variable. So, for each line of my data.frame I would like that the new data.frame have many rows as the population collumn. place<-c("place1", "place2", "place3", "place4", "place5") population<-c(100,200,300,50,30) my.df<-data.frame(cbind(place,population)) my.df best wishes, miltinho astronauta brazil [[alternative HTML version deleted]]
Try this: my.df <- data.frame(Place = rep(place, population), Population rep(population, population)) On Mon, Sep 29, 2008 at 2:26 PM, milton ruser <milton.ruser at gmail.com> wrote:> Dear all, > > I have a data.frame like the sample below, and I would > like to expand my data.frame using "population" variable. > So, for each line of my data.frame I would like that > the new data.frame have many rows as the population collumn. > > place<-c("place1", "place2", "place3", "place4", "place5") > population<-c(100,200,300,50,30) > my.df<-data.frame(cbind(place,population)) > my.df > > > best wishes, > > miltinho astronauta > brazil > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
try this: place <- c("place1", "place2", "place3", "place4", "place5") population <- c(100, 200, 300, 50, 30) my.df <- data.frame(place, population) my.df[rep(row.names(my.df), my.df$population), ] I hope it helps. Best, Dimitris milton ruser wrote:> Dear all, > > I have a data.frame like the sample below, and I would > like to expand my data.frame using "population" variable. > So, for each line of my data.frame I would like that > the new data.frame have many rows as the population collumn. > > place<-c("place1", "place2", "place3", "place4", "place5") > population<-c(100,200,300,50,30) > my.df<-data.frame(cbind(place,population)) > my.df > > > best wishes, > > miltinho astronauta > brazil > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
On Monday, September 29, 2008 1:59, Dimitris Rizopoulos wrote: > On Monday, September 29, 2008 1:26, milton ruser wrote: > > ...I have a data.frame like... > > place<-c("place1", "place2", "place3", "place4", "place5") > > population<-c(100,200,300,50,30) > > my.df<-data.frame(cbind(place,population)) > > ...and I would like to expand my data.frame > > using "population" variable. So, for each > > line of my data.frame I would like that the > > new data.frame have many rows as the > > population collumn... > ...try this... > place <- c("place1", "place2", "place3", "place4", "place5") > population <- c(100, 200, 300, 50, 30) > my.df <- data.frame(place, population) Or... place <- c("place1", "place2", "place3", "place4", "place5") population <- c(100, 200, 300, 50, 30) my.df <- data.frame(place, population) my.df [ rep ( row.names ( my.df ) , as.numeric ( as.character ( my.df$population ) ) ) , ] -- David