I have a data set like this:
> df<-data.frame(gender=c(rep("male",3),rep("female",3)),
Age=c(rep("old",3),rep("young",3)),VAR=c(rep(1:3),rep(1:3)),
FEN1=c(21,26,29,30,6,11),FEN2=c(14,55,12,33,9,21),
FEN3=c(88,23,55,23,14,66))
Where FEN1, FEN2 and FEN3 contain the total number of individuals belonging to that group and which have the characteristics of the columns VAR, Gender, Age, FEN.
And I need to change it to a data frame where each row belongs to one person (536 rows in total) with the characteristics of the columns VAR, Gender, Age.
The expected output would contain:
- 21 rows with information: male, old, 1, FEN1
- 14 rows with information: male, old, 1, FEN2
- 88 rows with information: male, old, 1, FEN3
- 26 rows with information: male, old, 2, FEN1
- 55 rows with information: male, old, 2, FEN2
- 23 rows with information: male, old, 2, FEN3
- and so on...
I was trying to do this by hand with a code like:
> df2<-as.data.frame(1:536)
> FEN <- c(rep("FEN1",123), rep("FEN2",144), rep("FEN3",269))
> df2$FEN<-FEN
> Gender<-c(rep("male",...)...
But obviously it is not at all efficient.