abn
abn

Reputation: 1373

R reshape converting all the values to 1

My original data looks like this

data
id1,id2,value
A,1,-1.09
B,1,-1.033
C,1,-1.5
A,2,9.1
B,2,2.006
C,2,-3.95

I tried using reshape package in R to reshape it using the command cast(data, id2 ~ id1) but this is converting all my values from value column to 1 like this

   A       B      C   
1  1       1      1 
2  1       1      1

when I actually want it to be like this. Can anyone please help me out.

  A       B      C   
1  -1.09   -1.033 -1.5
2  9.1     2.006  -3.95

EDIT: IT WORKED FOR ME when I added fun.aggregate = mean at the end of cast function.

Upvotes: 0

Views: 101

Answers (2)

abn
abn

Reputation: 1373

I had to add the aggregate function towards the end.

cast(data, id2 ~ id1,fun.aggregate = mean)

Upvotes: 0

nrussell
nrussell

Reputation: 18612

Try dcast in reshape2:

library(reshape2)
##
data <- read.table(
  text="id1 id2  value
A   1   -1.09
B   1   -1.033
C   1   -1.5
A   2   9.1
B   2   2.006
C   2   -3.95",
  header=TRUE)
##
> dcast(data,id2~id1)
  id2     A      B     C
1   1 -1.09 -1.033 -1.50
2   2  9.10  2.006 -3.95

Upvotes: 2

Related Questions