RBbg - Reshaping Time Series Data
Date : March 29 2020, 07:55 AM
it helps some times You could try read.zoo. Use index.column to specify in which column index/time is stored, and reshape data according to splitcolumnn, . The result is a zoo time series library(zoo)
z <- read.zoo(text = "ticker date PX_LAST
1 AUDUSD 2011-01-01 NA
2 AUDUSD 2011-01-02 NA
3 AUDUSD 2011-01-03 1.0205
4 AUDUSD 2011-01-04 1.0040
5 AUDUSD 2011-01-05 1.0014
6 AUDUSD 2011-01-06 0.9969
2127 EURUSD 2013-11-26 1.3557
2128 EURUSD 2013-11-27 1.3570
2129 EURUSD 2013-11-28 1.3596
2130 EURUSD 2013-11-29 1.3591
2131 EURUSD 2013-11-30 NA
2132 EURUSD 2013-12-01 NA", index.column = "date", split = "ticker")
z
# AUDUSD EURUSD
# 2011-01-01 NA NA
# 2011-01-02 NA NA
# 2011-01-03 1.0205 NA
# 2011-01-04 1.0040 NA
# 2011-01-05 1.0014 NA
# 2011-01-06 0.9969 NA
# 2013-11-26 NA 1.3557
# 2013-11-27 NA 1.3570
# 2013-11-28 NA 1.3596
# 2013-11-29 NA 1.3591
# 2013-11-30 NA NA
# 2013-12-01 NA NA
str(z)
|
reshaping daily time series data
Date : March 29 2020, 07:55 AM
it helps some times Extending Jason's / Dominic's solution this gives you an example of how to plot your data as a xts time series as you asked for: library(xts)
dat<-read.csv('~/Downloads/stack_a.csv')
dat.m <-reshape(dat,direction='long',idvar=c('year','month'),varying=list(3:33),v.names='value')
dat.m <- dat.m[order(dat.m[,1],dat.m[,2],dat.m[,3]),] # order by year, month, day(time)
dat.m$date <-paste0(dat.m$year,'-',dat.m$month,'-',dat.m$time) # concatenate these 3 columns
dat.m <- na.omit(dat.m) # remove the NAs introduced in the original data
dat.xts <- as.xts(dat.m$value,order.by = as.Date(dat.m$date))
names(dat.xts) <- 'value'
plot(dat.xts)
|
Reshaping data of different time lengths in R
Tag : r , By : David Marchant
Date : March 29 2020, 07:55 AM
will be helpful for those in need Here is one idea. dt_all is the final output. Notice that this example does not create Timex, Timey, and Timez, but I would argue that one column called Time is sufficient and individual Timex, Timey, and Timez are redundant. # Load packages
library(dplyr)
library(tidyr)
# Process the data
dt_all <- dt %>%
gather(Var, Value, -ID, -Group) %>%
mutate(Time = sub("[a-z]", "", Var), Type = sub("[0-9]", "", Var)) %>%
select(-Var) %>%
spread(Type, Value)
# Create example data frames
dt <- read.table(text = "ID Group x1 x2 x3 y1 y2 y3 z1 z2
144 1 566 613 597 563 549 562 599 469
167 2 697 638 756 682 695 693 718 439.5
247 4 643 698 730 669 656 669 698 514.5
317 4 633 646 641 520 543 586 559 405.5
344 3 651 678 708 589 608 615 667 514
352 2 578 702 671 536 594 579 591 467.5
382 1 678 690 693 555 565 534 521 457.5
447 3 668 672 718 663 689 751 784 506.5
464 2 760 704 763 514 554 520 564 486
628 1 762 789 783 618 610 645 625 536",
header = TRUE)
|
Reshaping data with no time var
Date : March 29 2020, 07:55 AM
help you fix your problem I wonder if the tableone package might help you here. Consider: data$sex <- factor(data$sex) # note that you will have to ensure these are factors
data$country <- factor(data$country)
library(tableone)
tab1 <- CreateTableOne(vars=c("sex", "country"), strata="job", data=data)
print(tab1, showAllLevels=TRUE, test=FALSE, explain=FALSE)
# Stratified by job
# level 11 12 13
# n 2 2 1
# sex 0 2 (100.0) 0 ( 0.0) 1 (100.0)
# 1 0 ( 0.0) 2 (100.0) 0 ( 0.0)
# country 1 1 ( 50.0) 0 ( 0.0) 1 (100.0)
# 2 0 ( 0.0) 2 (100.0) 0 ( 0.0)
# 3 1 ( 50.0) 0 ( 0.0) 0 ( 0.0)
out.data <- t(sapply(split(data, job), function(df){
with(df, c(table(sex), table(country))) }))
out.data <- data.frame(job=rownames(out.data), out.data)
rownames(out.data) <- NULL
colnames(out.data)[2:6] <- c(paste("sex", levels(data$sex), sep="_"),
paste("country", levels(data$country), sep="_") )
out.data
# job sex_0 sex_1 country_1 country_2 country_3
# 1 11 2 0 1 0 1
# 2 12 0 2 0 2 0
# 3 13 1 0 1 0 0
|
Reshaping name and time data from (name within each time) to (all times for each name)
Tag : r , By : user171555
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I'm trying to reshape a sign up sheet for an event using R. , You can use aggregate > aggregate(V2~V1, data=df, FUN=paste0)
V1 V2
1 xxx@email.com 9-11, 3-5
2 yyy@email.com 9-11, 11-1, 1-3, 3-5
3 zzz@email.com 11-1, 1-3
df <- read.table(text="xxx@email.com 9-11
yyy@email.com 9-11
zzz@email.com 11-1
yyy@email.com 11-1
zzz@email.com 1-3
yyy@email.com 1-3
xxx@email.com 3-5
yyy@email.com 3-5", header=FALSE)
|