[R] Regroup and create new dataframe

David L Carlson dc@r|@on @end|ng |rom t@mu@edu
Fri Jun 1 20:55:47 CEST 2018

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:

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)
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 <r-help-bounces using r-project.org> On Behalf Of nguy2952 University of Minnesota
Sent: Friday, June 1, 2018 10:54 AM
To: 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

Here is what I did.


   I split the original data frame which I called data1:

   X = split(data1, Product_name)


   Unlist X

   X1 = unlist(X)


   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?


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?*


*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.

Hugh N

	[[alternative HTML version deleted]]

R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list