0

I have a set of date-bound values, like:

2015-01-01 1
2015-01-02 4

etc.

I would like to compare different years data by "normalizing" it like this:

              Value2014    Value2015
1900-01-01      1             5
1900-01-02                    4

etc.

This "Value2014/2015" columns sort of make me "shame on me".. What if tomorrow I'll have another set of years to process ? That should not be so much hardcoded, I feel

Would not it be more "pretty" (not know how much practical, but still), to have it like that:

                 Value 
1900-01-01      (1;5)
1900-01-02      (;4)

etc.

I've played with:

v <- list()
v[[1]] <- c(1,2,3)
v[[2]] <- c(1,0,0)
dv <- data.frame(dates = c("2014-01-02","2014-01-03"),values = v)

...and it ends with

Error in data.frame(dates = c("2014-01-02", "2014-01-03"), values = v) : arguments imply differing number of rows: 2, 3

Apparently, data.frame "parses into" v and tries to convert v[[1]] and v[[2]] as separate columns.

Should I "forget about that" or are there more appropriate expressions for this ?

Thanks!

62mkv
  • 1,444
  • 1
  • 16
  • 28
  • 1
    You were pretty close, but try `v[[1]] <- list(1,2,3)` and `v[[2]] <- list(1,0,0)`, and then `dv <- data.frame(dates = c("2014-01-02","2014-01-03"),values = I(v))`. See the link above for an explanation. – slamballais Feb 18 '16 at 16:00
  • Sorry if I misuse comments here, but my real problem was not solved by this... I see how to construct a data.frame "from scratch", but what I really need is to have an array(list) as a value of a column X for every row ! And I really lost a lot of hair trying to achieve it: NO LUCK so far. The best I come close to is: ds$SpY <- replicate(123,array(NA, dim = length(years), dimnames=years)) but even this is producing an error: Error in `$<-.data.frame`(`*tmp*`, "SpY", value = c(NA, NA, NA, NA, NA, : replacement has 4 rows, data has 123 What can I do with this ?? – 62mkv Feb 23 '16 at 13:41
  • It's not entirely clear what you are trying to do or how that is different from your opening post. Also, at this point, it is probably better to start a new question, as this post is now rather old. – slamballais Feb 23 '16 at 15:48
  • Thanks Laterow, I really appreciate your help. I think it's actually better to say that this question is a duplicate indeed, and possibly start a new question. – 62mkv Feb 25 '16 at 04:37

0 Answers0