birdbox05
birdbox05

Reputation: 17

Separating unique values in a column into separate columns within the same dataframe

I have data in which total number of fruits consumed in 2010 and 2011 are listed. However at the moment, the totals for each year are joined at the bottom of the data frame. Instead I want the totals for each year listed in a new column.

My current data frame looks like this:

Fruit Total Year
Apple 863 2010
Banana 224 2010
Orange 455 2010
Apple 934 2011
Banana 453 2011
Orange 534 2011

However I want it to look like this instead:

Fruit 2010 2011
Apple 863 934
Banana 224 453
Orange 455 534

I am trying to do this using R

Upvotes: 0

Views: 447

Answers (3)

akrun
akrun

Reputation: 887078

Or can use xtabs from base R

xtabs(Total ~ Fruit + Year, df)
#        Year
#Fruit    2010 2011
#  Apple   863  934
#  Banana  224  453
#  Orange  455  534

data

df <- structure(list(Fruit = c("Apple", "Banana", "Orange", "Apple", 
"Banana", "Orange"), Total = c(863L, 224L, 455L, 934L, 453L,
534L), Year = c(2010L, 2010L, 2010L, 2011L, 2011L, 2011L)),
class = "data.frame", row.names = c(NA,
-6L))

Upvotes: 0

ThomasIsCoding
ThomasIsCoding

Reputation: 101317

A base R option using reshape

reshape(
  df,
  direction = "wide",
  idvar = "Fruit",
  timevar = "Year"
)

gives

   Fruit Total.2010 Total.2011
1  Apple        863        934
2 Banana        224        453
3 Orange        455        534

#Data

> dput(df)
structure(list(Fruit = c("Apple", "Banana", "Orange", "Apple", 
"Banana", "Orange"), Total = c(863L, 224L, 455L, 934L, 453L,
534L), Year = c(2010L, 2010L, 2010L, 2011L, 2011L, 2011L)), class = "data.frame", row.names = c(NA,
-6L))

Upvotes: 1

Clarius333
Clarius333

Reputation: 93

You probably just want to convert from long to wide format, which can be done in multiple ways.

One example is with using tidyr

By running the code below, you create a new DF_wide from your original wide dataframe should count the totals per year in separate columns.

library(tidyr)

DF_wide <- spread(DF_long, Year, Fruit, Total)

Upvotes: 0

Related Questions