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, 2022 in Data Science by Avinash
• 1,260 points
454 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

Unleash the power of data with our comprehensive Data Science Training.

answered Jun 24, 2022 by Sohail
• 3,040 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

Jun 24, 2022 in Data Science by Sohail
• 3,040 points
358 views
0 votes
1 answer

How to establish a connection to a remote Redash database using R?

To connect to a Redash database using ...READ MORE

answered Feb 17, 2023 in Data Science by anonymous
652 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, 2022 in Data Science by gaurav
• 23,260 points
1,280 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, 2022 in Data Science by avinash
• 1,840 points
372 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,570 points
1,030 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
1,178 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
5,890 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
1,112 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, 2022 in Data Science by Sohail
• 3,040 points
523 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, 2022 in Data Science by Sohail
• 3,040 points
1,037 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP