RBasic1, RBasic2, RBasic3, data.table, dplyr, tidyr, RGIS, Leaflet
library(tidyr)
df <- data.frame(
id = 1:10,
time = as.Date('2009-01-01') + 0:9,
Q3.2.1. = rnorm(10, 0, 1),
Q3.2.2. = rnorm(10, 0, 1),
Q3.2.3. = rnorm(10, 0, 1),
Q3.3.1. = rnorm(10, 0, 1),
Q3.3.2. = rnorm(10, 0, 1),
Q3.3.3. = rnorm(10, 0, 1)
)
df %>%
gather(key, value, -id, -time) %>%
extract(key, c("question", "loop_number"), "(Q.\\..)\\.(.)") %>%
spread(question, value)
## id time loop_number Q3.2 Q3.3
## 1 1 2009-01-01 1 1.48026187 0.26571275
## 2 1 2009-01-01 2 0.27609905 0.06463176
## 3 1 2009-01-01 3 -0.69430568 0.31914386
## 4 2 2009-01-02 1 -0.03316801 -1.66106639
## 5 2 2009-01-02 2 -0.73530047 -0.19701599
## 6 2 2009-01-02 3 0.48615698 -1.03575199
## 7 3 2009-01-03 1 -0.78808407 -0.62842204
## 8 3 2009-01-03 2 0.40572720 -0.67688161
## 9 3 2009-01-03 3 0.64719939 -0.73808721
## 10 4 2009-01-04 1 0.66968925 1.32140734
## 11 4 2009-01-04 2 0.56013278 1.41560262
## 12 4 2009-01-04 3 1.84735364 0.52735973
## 13 5 2009-01-05 1 -0.86636166 0.59765059
## 14 5 2009-01-05 2 -1.01007402 2.53071388
## 15 5 2009-01-05 3 2.12784274 -1.75902866
## 16 6 2009-01-06 1 1.78973854 -1.07990443
## 17 6 2009-01-06 2 -0.54954327 -1.80335041
## 18 6 2009-01-06 3 -0.69057731 0.53363774
## 19 7 2009-01-07 1 0.44446013 0.59680775
## 20 7 2009-01-07 2 0.32656188 1.42494152
## 21 7 2009-01-07 3 0.88739977 -0.97357228
## 22 8 2009-01-08 1 0.15236122 0.85031652
## 23 8 2009-01-08 2 -1.39518403 1.40377763
## 24 8 2009-01-08 3 -1.65153910 -0.44241794
## 25 9 2009-01-09 1 -0.80401083 -0.10124949
## 26 9 2009-01-09 2 -0.11714964 0.80798094
## 27 9 2009-01-09 3 1.18367957 -0.04020107
## 28 10 2009-01-10 1 -0.64863217 0.37386287
## 29 10 2009-01-10 2 -2.12244405 2.26434736
## 30 10 2009-01-10 3 -0.31576125 -0.25918304
stocks <- data.frame(
time = as.Date('2009-01-01') + 0:9,
X = rnorm(10, 0, 1),
Y = rnorm(10, 0, 2),
Z = rnorm(10, 0, 4)
)
stocks %>% gather(stock, price, -time)
## time stock price
## 1 2009-01-01 X -2.89990950
## 2 2009-01-02 X 0.58971723
## 3 2009-01-03 X 0.01082860
## 4 2009-01-04 X -0.35477691
## 5 2009-01-05 X -1.48079936
## 6 2009-01-06 X -0.07148039
## 7 2009-01-07 X 0.50547193
## 8 2009-01-08 X -0.54977666
## 9 2009-01-09 X -0.54160436
## 10 2009-01-10 X 0.60799995
## 11 2009-01-01 Y -3.57196766
## 12 2009-01-02 Y 0.96470192
## 13 2009-01-03 Y 3.16596114
## 14 2009-01-04 Y 0.55584147
## 15 2009-01-05 Y -2.07898307
## 16 2009-01-06 Y -2.67322277
## 17 2009-01-07 Y 4.48944764
## 18 2009-01-08 Y -2.95525351
## 19 2009-01-09 Y 3.20677725
## 20 2009-01-10 Y 0.46091824
## 21 2009-01-01 Z -2.98789746
## 22 2009-01-02 Z 7.78323734
## 23 2009-01-03 Z 3.73236971
## 24 2009-01-04 Z 5.67454259
## 25 2009-01-05 Z 4.50772886
## 26 2009-01-06 Z -9.36186309
## 27 2009-01-07 Z 3.10702335
## 28 2009-01-08 Z 6.19676355
## 29 2009-01-09 Z -0.61963421
## 30 2009-01-10 Z -2.49611685
stocksm <- stocks %>% gather(stock, price, -time)
stocksm %>% spread(stock, price)
## time X Y Z
## 1 2009-01-01 -2.89990950 -3.5719677 -2.9878975
## 2 2009-01-02 0.58971723 0.9647019 7.7832373
## 3 2009-01-03 0.01082860 3.1659611 3.7323697
## 4 2009-01-04 -0.35477691 0.5558415 5.6745426
## 5 2009-01-05 -1.48079936 -2.0789831 4.5077289
## 6 2009-01-06 -0.07148039 -2.6732228 -9.3618631
## 7 2009-01-07 0.50547193 4.4894476 3.1070234
## 8 2009-01-08 -0.54977666 -2.9552535 6.1967636
## 9 2009-01-09 -0.54160436 3.2067772 -0.6196342
## 10 2009-01-10 0.60799995 0.4609182 -2.4961169
stocksm %>% spread(time, price)
## stock 2009-01-01 2009-01-02 2009-01-03 2009-01-04 2009-01-05 2009-01-06
## 1 X -2.899909 0.5897172 0.0108286 -0.3547769 -1.480799 -0.07148039
## 2 Y -3.571968 0.9647019 3.1659611 0.5558415 -2.078983 -2.67322277
## 3 Z -2.987897 7.7832373 3.7323697 5.6745426 4.507729 -9.36186309
## 2009-01-07 2009-01-08 2009-01-09 2009-01-10
## 1 0.5054719 -0.5497767 -0.5416044 0.6079999
## 2 4.4894476 -2.9552535 3.2067772 0.4609182
## 3 3.1070234 6.1967636 -0.6196342 -2.4961169