-1

My data looks like as follows,

Date         Industry    Indices
2014/12/03   A           2.3
2014/12/03   B           3.4
2014/12/03   C           4.2
2014/12/03   D           3.0
2014/12/03   E           1.8
2014/12/04   A           2.7
2014/12/04   B           3.4
2014/12/04   C           4.5
2014/12/04   D           3.1
2014/12/04   E           2.1
2014/12/05   A           3.1
2014/12/05   B           2.5
2014/12/05   C           3.5
2014/12/05   D           3.1
2014/12/05   E           1.9

What I want is like this,

Date         A     B    C    D    E
2014/12/03   2.3   3.4  4.2  3.0  1.8
2014/12/04   2.7   3.4  4.5  3.1  2.1
2014/12/05   3.1   2.5  3.5  3.1  1.9
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
user3566160
  • 21
  • 2
  • 5
  • Not sure why are you calling it "extract", but this a very simple action with many dupes in SO, for example `library(reshape2) ; dcast(df, Date ~ Industry)` – David Arenburg Dec 30 '14 at 08:11
  • What you're looking for is called a "pivot" or "pivoting". I'm sure there are several questions about this already here on SO. – fancyPants Dec 30 '14 at 08:12

1 Answers1

0

You could try

library(tidyr)
spread(df, Industry, Indices)
#        Date   A   B   C   D   E
#1 2014/12/03 2.3 3.4 4.2 3.0 1.8
#2 2014/12/04 2.7 3.4 4.5 3.1 2.1
#3 2014/12/05 3.1 2.5 3.5 3.1 1.9

Or using base R

reshape(df, idvar='Date', timevar='Industry', direction='wide')

data

df <- structure(list(Date = c("2014/12/03", "2014/12/03", "2014/12/03", 
"2014/12/03", "2014/12/03", "2014/12/04", "2014/12/04", "2014/12/04", 
"2014/12/04", "2014/12/04", "2014/12/05", "2014/12/05", "2014/12/05", 
"2014/12/05", "2014/12/05"), Industry = c("A", "B", "C", "D", 
"E", "A", "B", "C", "D", "E", "A", "B", "C", "D", "E"), Indices = c(2.3, 
3.4, 4.2, 3, 1.8, 2.7, 3.4, 4.5, 3.1, 2.1, 3.1, 2.5, 3.5, 3.1, 
1.9)), .Names = c("Date", "Industry", "Indices"), class = "data.frame",
row.names = c(NA, -15L))
akrun
  • 874,273
  • 37
  • 540
  • 662