[R] Transform a dataset from long to wide using reshape2

John Kane jrkrideau at yahoo.ca
Sat Feb 26 20:11:51 CET 2011


I seem to be running into the same problem reported in 
https://stat.ethz.ch/pipermail/r-help/2010-November/258265.html

I cannot seem to transform a dataset from long to wide using reshape2.
Clearly I am missing something very simple but a look at the manual and the reshape paper in JSS does not suggest anything.

Any advice would be welcome
===========================load rehape2====================
library(reshape2)
========================Working example===========================
# Example from above URL
dat <- read.table(textConnection('Subject   Item Score
"Subject 1" "Item 1"     1
"Subject 1" "Item 2"     0
"Subject 1" "Item 3"     1
"Subject 2" "Item 1"     1
"Subject 2" "Item 2"     1
"Subject 2" "Item 3"     0'), header=TRUE)
closeAllConnections()

acast(dat, Subject~Item)

# Seems fine
=====================My attemp===========================
# 
set.seed(1)
mydata <- data.frame( x=sample(LETTERS[23:26],100, replace=TRUE),
        y=rnorm(100, mean=2), id=sample(letters[1:4], 100, replace=TRUE))
   
acast(mydata , id ~ x)
# not so fine
=================================================================
 produces this : 
 Using id as value column: use value_var to override.
Aggregation function missing: defaulting to length
  W  X Y  Z
a 7  5 5  7
b 6  9 6 12
c 1  4 5  6
d 6 14 5  2

Why does reshape2 think it needs an aggregation function here>

Thanks



More information about the R-help mailing list