[R] Regroup and create new dataframe

David L Carlson dc@r|@on @end|ng |rom t@mu@edu
Fri Jun 1 21:09:43 CEST 2018


Responses should be copied to r-help using ReplyAll. You are still sending html formatted emails. If you are using Microsoft Outlook, click the Format Text tab and select “Aa Plain Text”. No one has asked you to reveal the data set, only to create one with a similar structure. Is the data I sent reasonably close? What should it look like after it is transformed? 

David C

From: nguy2952 University of Minnesota <nguy2952 using umn.edu> 
Sent: Friday, June 1, 2018 1:57 PM
To: David L Carlson <dcarlson using tamu.edu>
Subject: Re: [R] Regroup and create new dataframe

Hi,
This is not an assignment for school.
This is a project at WORK. 
I am not allowed to reveal the dataset.

Thanks!

On Fri, Jun 1, 2018 at 1:55 PM, David L Carlson <mailto:dcarlson using tamu.edu> wrote:
Your question raises several issues. First, we do not do homework here, so if this is an assignment, you will not get much help. Second, you need to send your emails as plain text, not html. Third, you need to provide a reproducible example and send your data using dput() so that we can follow what you have tried so far. For example, here's a data set that resembles what you have described:

set.seed(42)
Tape <- data.frame(Year=2011:2015, Product=rep(c("Target", "3M", "Avery"),
     each=5), Sales=sample(1000:2000, 15), Region=rep(c("North", "South",
     "West"), each=5), stringsAsFactors=FALSE)
dput(Tape)
structure(list(Year = c(2011L, 2012L, 2013L, 2014L, 2015L, 2011L, 
2012L, 2013L, 2014L, 2015L, 2011L, 2012L, 2013L, 2014L, 2015L
), Product = c("Target", "Target", "Target", "Target", "Target", 
"3M", "3M", "3M", "3M", "3M", "Avery", "Avery", "Avery", "Avery", 
"Avery"), Sales = c(1915L, 1937L, 1285L, 1828L, 1639L, 1517L, 
1732L, 1133L, 1652L, 1699L, 1453L, 1711L, 1924L, 1252L, 1456L
), Region = c("North", "North", "North", "North", "North", "South", 
"South", "South", "South", "South", "West", "West", "West", "West", 
"West")), .Names = c("Year", "Product", "Sales", "Region"), row.names = c(NA, 
-15L), class = "data.frame")

It is not clear what you want in your new data frame. This one has 5 years of data for each tape brand and you seem to want one row for each tape brand? Tables created in html and then sent to a plain text mailing list can be dramatically different from the original format. It is not clear that you cannot answer your questions from the data as presented here. Look at the results of unlist(split(Tape, Tape$Product)). You should see that this is nowhere near what you described.

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352

-----Original Message-----
From: R-help <mailto:r-help-bounces using r-project.org> On Behalf Of nguy2952 University of Minnesota
Sent: Friday, June 1, 2018 10:54 AM
To: mailto:r-help using r-project.org
Subject: [R] Regroup and create new dataframe

Hello folks,

I have a big project to work on and the dataset is classified so I am just going to use my own example so everyone can understand what I am targeting.

Let's take Target as an example: We consider three brands of tape: Target brand, 3M and Avery. The original data frame has 4 columns: Year of Record, Product_Name(which contains three brands of tape), Sales, and Region. I want to create a new data frame that looks like this:

                      Year of Record       Sales     Region
  Target Brand
  3M
  Avery

Here is what I did.

   1.

   I split the original data frame which I called data1:

   X = split(data1, Product_name)

   2.

   Unlist X

   X1 = unlist(X)

   3.

   Create a new data frame

   new_df = as.data.frame(X1)


But, when I used the command View(new_df), I had only two columns: The left one is similar to TargetBrand.Sales, etc. and the right one is just "X1"

I did not achieve what I wanted.

**A potentially big question from readers:*

Why am I doing this?

*Answer:*

I want to run a multiple regression model later to see among different regions, what the sales look like for these three brands of tape:

*Does Mid-west buy more house brand than East Coast?*

or

*Does region really affect the sales? Are Mid-West's purchases similar to those of East Coast and West Coast?*

I need help. Please give me guidance.

Sincerely,
Hugh N

        [[alternative HTML version deleted]]

______________________________________________
mailto:R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwMFaQ&c=ODFT-G5SujMiGrKuoJJjVg&r=veMGHMCNZShld-KX-bIj4jRE_tP9ojUvB_Lqp0ieSdk&m=LnVfoBf5smekeCFlal5rmpELFRoDrB3H3ij_lZJRy0w&s=nPZ3F6nROsY3KM0z7y6ixAAYLjMGVhEZyuXMi3bg0rY&e=
PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwMFaQ&c=ODFT-G5SujMiGrKuoJJjVg&r=veMGHMCNZShld-KX-bIj4jRE_tP9ojUvB_Lqp0ieSdk&m=LnVfoBf5smekeCFlal5rmpELFRoDrB3H3ij_lZJRy0w&s=XNpVm_6i2GLFLk3FzpM9T15aezUet1BA0FlapuVXdmc&e=
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list