[R] creating individual records from a frequency distribution

Gavin Rudge G.Rudge at bham.ac.uk
Wed Oct 22 17:48:43 CEST 2014


I've got data frame containing a simple frequency distribution of numbers of people in three age groups by area.

df1<-data.frame(area=c(1,2),group1=c(2,3),group2=c(1,5),group3=c(4,0))
df1

I want to get a data frame with one record per person (in my case 15 of them) which would look like this, with variables indicating the area and age group to which each belongs

df2<-data.frame(person_id=seq(1:15),area=c(rep(1,7),rep(2,8)),group_num=c(1,1,2,3,3,3,3,1,1,1,2,2,2,2,2))
df2

This is not the same as melting wide to long data as in reshape2, as I'm melting from aggregated data. I can get vectors of columns values using the rep command, but sewing them together and allowing for zeros looks a bit cumbersome. I'm assuming there is a simple command that does this sort of thing.

Any help gratefully received,

GavinR

	[[alternative HTML version deleted]]



More information about the R-help mailing list