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
13 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 Jun 24 by Sohail
• 2,960 points

Related Questions In Data Science

0 votes
0 answers
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
• 17,580 points
119 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
• 1,840 points
17 views
0 votes
1 answer

How to join (merge) data frames (inner, outer, left, right)

The merge function and its optional parameters ...READ MORE

answered Jun 24 in Data Science by Sohail
• 2,960 points
23 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
437 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
482 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,352 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
504 views
0 votes
1 answer

Changing column names of a data frame

import pandas as pd #define DataFrame df = pd.DataFrame({'team':['A', ...READ MORE

answered Jun 24 in Data Science by Sohail
• 2,960 points
18 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,960 points
58 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