1

I have a requirement of creating pivot table from dataframe given below :

enter image description here

Kindly help me to get pivot table as given below (I have copied from excel) :

enter image description here

I need exactly given as using R.

To generate the similar set of dataset you can use the command given below :

enter code here

Data1 <- data.frame(
X = sample(1:10),
Y = sample(1:10), 
Z = sample(1:10),
count= sample(11:20))

Thanks in advance !

Vishal
  • 189
  • 3
  • 11
  • 1
    Can you provide a working example? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Feb 25 '14 at 09:47
  • Here, is the code, using it you can create the similar dataframe which i have described above : Data1 <- data.frame( X = sample(1:10), Y = sample(1:10), Z = sample(1:10), count= sample(11:20)) – Vishal Feb 25 '14 at 10:24
  • The package `reshape` could be useful in most of the pivots. – Vyga Feb 25 '14 at 14:20

1 Answers1

2

Try ftable:

exData <- setNames(as.data.frame(unique(t(combn(rep(1:3, 3), m=3)))), paste0("P", 1:3))
ftable(exData)
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Thanks Luke, i can able to create the pivot table using ftable function, though i didn't understand the logic used in while saving dataframe in exData. can you please explain the same? Thanks in Advance ! – Vishal Feb 25 '14 at 10:56
  • Can you expand your answer by elaborating each step? – Roman Luštrik Feb 25 '14 at 12:53