0

In R i have a matrix that has several categorical values to it. Indexed size 2sqm, 4 sqm, 6sqm, number of units from 1-3, number of persons from 1-4 and then a column that has a summarized count from all the occurrences.

ex:

Size;Units;Pers;Count
4;3;4;3 # three time this row
2;1;1;2 # two times this row
6;2;2;1 # one times this row

How can i make the last column/vector multyply the rows so that is prints out:

Size;Units;Pers;Count
4;3;4;1
4;3;4;1
4;3;4;1
2;1;1;1
2;1;1;1
6;2;2;1

Either in spreadsheet or in R. This is a assignment for school and i just cannot find the way to make the last vector (which i use as a constant to multiply the first 3 columns and yet still keep one in the last column entry.

MichaelR
  • 152
  • 8
  • Well, thanks to @SamFirke, you should learn that even if you can't figure it out by yourself, you should at least be able to research around because most often the answer is out there. – Shawn Mehan Oct 01 '15 at 16:31

1 Answers1

1

We can replicate the sequence of rows by the 'Count' column and transform to create the 'Count' column of 1.

transform(df1[rep(1:nrow(df1), df1$Count),-4], Count=1)

This can be also done with wrapper function expandRows from library(splitstackshape)

library(splitstackshape)
transform(expandRows(df1, 'Count'), Count=1)
akrun
  • 874,273
  • 37
  • 540
  • 662