Using dcast to widen a data frame

0 votes
I've got the following data set:

z - data.frame(item=letters[rep(24:26,2)], freq=c(4,3,2,4,4,1), id=rep(1:2,each=3), freq=c(4,3,2,4,4,1), id=rep(1:2,each=3) )

item freq id multiplied by four 1 z 2 y 3 y 3 y 3 y 3 y 3 y 3 1 x 4 2 y 4 2 z 1 2 x 4 2 y 4 2 z 1 2 x 4 2 y 4 2
Every id/item combination results in a unique data frame.

This is how I'd like it to be:

id x y z id x y z id x y
1 4 3 2 2 4 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
This appears to be a fairly straightforward transformation, but I can't manage to get it to work.

Here's what I came up with (the data frame's name is z):

dcast(z,id,item,sum) yields the following:

1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
What am I doing incorrectly?
Jun 19 in Data Science by Avinash
• 1,260 points
7 views

1 answer to this question.

0 votes
> dcast(z, id ~ item, value.var="freq")
  id x y z
1  1 4 3 2
2  2 4 4 1
answered 4 days ago by Sohail
• 2,280 points

Related Questions In Data Science

0 votes
0 answers

To speed up the tapply function in R, or another function to convert data frame into a matrix

I must turn a sizable dataset into ...READ MORE

4 days ago in Data Science by Sohail
• 2,280 points
2 views
0 votes
0 answers

Changing column names of a data frame

I have a data frame called "newprice" ...READ MORE

Jun 14 in Data Science by Avinash
• 1,260 points
8 views
0 votes
1 answer

Converting xlsx to xls using Microsoft Office Compitablity Pack's excelcnv

Simply rearranging the positions of the parameters ...READ MORE

answered Mar 25 in Data Science by gaurav
• 12,980 points
73 views
0 votes
0 answers

Determine the data types of a data frame's columns

I'm using R and have loaded data ...READ MORE

Jun 1 in Data Science by avinash
• 860 points
12 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
422 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
467 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
4,280 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
491 views
0 votes
1 answer

How to make loop for one-at-a time logistic regression in R?

You're probably looking for something similar to ...READ MORE

answered Jun 20 in Data Science by Sohail
• 2,280 points
16 views
0 votes
1 answer

How to implement Knn-algorithm without using k-nn function in r?

I created an example that demonstrates the ...READ MORE

answered 4 days ago in Data Science by Sohail
• 2,280 points
7 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP