-3

I have data from a CSV file in the following format:

Timestamp  variable value
03/10/2014 var1  10
04/10/2014 var1  11
01/10/2014 var2  5
02/10/2014 var2  16
03/10/2014 var2  17
04/10/2014 var2  18

And I want to get the data in the following format:

Timestamp var1 var2
01/10/2014  NaN  5
02/10/2014  NaN  16
03/10/2014  10   17
04/10/2014  11   18

I want to reshape this data in R. Any suggestion please.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
alaa goupi
  • 21
  • 2
  • This looks like a code request for _two_ languages? :( – miradulo Feb 26 '17 at 00:34
  • What have you tried? I think this is pretty simple using the tidyverse. It looks like you are just going from long to wide. – Elin Feb 26 '17 at 00:47
  • sorry, i am a beginner in using r, so I really have no idea – alaa goupi Feb 26 '17 at 00:51
  • Alaa, @Elin is referring to Wickham's paper http://vita.had.co.nz/papers/tidy-data.pdf . Give that a read and see if you can do some long-to-wide conversion. If you don't have a specific question, StackOverflow will probably not be friendly to you, but the `?` in R works for finding example snippets if you get stuck (like, `?data.table`). – bright-star Feb 26 '17 at 00:58
  • Also just try googling "r long to wide" and you will find lots of information. Once you have tried that, if you have a question or problem, it will make sense to ask. ALso look at http://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – Elin Feb 26 '17 at 01:02

1 Answers1

0

In R you can use dcast

library(reshape2)
df <- rbind.data.frame(c("01/10/2014", "var1", 10),
        c("02/10/2014", "var1", 11),
        c("01/10/2014", "var2", 5),
        c("02/10/2014", "var2", 16))
colnames(df) <- c("Timestamp", "variable", "value")
dcast(df, Timestamp~variable)
#    Timestamp var1 var2
# 1 01/10/2014   10    5
# 2 02/10/2014   11   16

Since you are a beginner user, I would also recommend looking at the melt function to perform the opposite

df2 <- dcast(df, Timestamp~variable)
melt(df2, id.vars="Timestamp")
#    Timestamp variable value
# 1 01/10/2014     var1    10
# 2 02/10/2014     var1    11
# 3 01/10/2014     var2     5
# 4 02/10/2014     var2    16
Djork
  • 3,319
  • 1
  • 16
  • 27