3

I have my data like that

windspeed      month
1.2             Jan
2               Feb
3.5             Mar
2.6             Apr
5.9             Jun
2.5             Jul

I want to get this kind of data

Jan   Feb   Mar   Apr    Jun   Jul  
1.2    2     3.5    2.6   5.9   2.5  
Chris Watson
  • 1,347
  • 1
  • 9
  • 24
  • 2
    [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – zero323 Jul 11 '15 at 20:13

4 Answers4

4

just base R (even though setNames belongs to stats package in fact):

setNames(dat$windspeed, dat$month)
Jan Feb Mar Apr Jun Jul 
1.2 2.0 3.5 2.6 5.9 2.5 

yes there is (are) a way(s), here is one:

as.data.frame(t(setNames(dat$windspeed, dat$month)))
  Jan Feb Mar Apr Jun Jul
1 1.2   2 3.5 2.6 5.9 2.5
SabDeM
  • 7,050
  • 2
  • 25
  • 38
4

What about

t(unstack(DF, windspeed ~ month))

    Apr Feb Jan Jul Jun Mar
res 2.6   2 1.2 2.5 5.9 3.5
Chris Watson
  • 1,347
  • 1
  • 9
  • 24
2

There are some very useful packages as well in R, suitable for such operations like reshape, data.table, tidyr

library(reshape2)
library(data.table)

dcast.data.table(melt(setDT(data), id.vars = "month"), variable ~ month)
#   variable Apr Feb Jan Jul Jun Mar
#1: windspeed 2.6   2 1.2 2.5 5.9 3.5

library(reshape2)
library(tidyr)
spread(melt(data), month, value)

#   variable Apr Feb Jan Jul Jun Mar
#1 windspeed 2.6   2 1.2 2.5 5.9 3.5
Veerendra Gadekar
  • 4,452
  • 19
  • 24
0

You could try this:

DF <- data.frame(windspeed=c(1.2, 2, 3.5, 2.6, 5.9, 2.5), month=c('Jan', 'Feb', 'Mar', 'Apr', 'Jun', 'Jul'))
DF <- t(DF)
colnames(DF) = DF[2, ]
DF <- DF[1,]
DF

Gives you

 Jan   Feb   Mar   Apr   Jun   Jul 
"1.2" "2.0" "3.5" "2.6" "5.9" "2.5"
tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149