logo
down
shadow

reshaping data with time represented as spells


reshaping data with time represented as spells

Content Index :

reshaping data with time represented as spells
Tag : r , By : quasarkitten
Date : November 26 2020, 04:01 AM

it helps some times I have a dataset in which time is represented as spells (i.e. from time 1 to time 2), like this: , You were almost there.
Code
d %>%
    # Gather the needed variables. Explanation:
    # t_type: How will the call the column where we will put the former
    #         variable names under?
    # t:      How will we call the column where we will put the
    #         values of above variables?
    # -id,
    # -value: Which columns should stay the same and NOT be gathered
    #         under t_type (key) and t (value)?
    # 
    gather(t_type, t, -id, -value) %>%
    # Select the right columns in the right order. 
    # Watch out: We did not select t_type, so it gets dropped.
    select(id, t, value) %>%
    # Arrange / sort the data by the following columns.
    # For a descending order put a "-" in front of the column name.  
    arrange(id, t)
   id t value
1   A 1     1
2   A 2     1
3   A 3     2
4   A 4     2
5   B 1     3
6   B 2     3
7   B 3     4
8   B 4     4
9   C 1     5
10  C 2     5
11  C 3     6
12  C 4     6

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

RBbg - Reshaping Time Series Data


Tag : r , By : T11M
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


Tag : r , By : ChrisMe
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


Tag : r , By : Giles
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)
Related Posts Related QUESTIONS :
  • dplyr join on column A OR column B
  • Replace all string if row starts with (within a column)
  • Is there a possibility to combine position_stack and nudge_x in a stacked bar chart in ggplot2?
  • How can I extract bounding boxes in a row-wise manner using R?
  • How do I easily sum up values in different columns?
  • Reading numeric Date value from CSV file to data.frame in "R"
  • R programming: creating a stacked bar graph, with variable colors for each stacked bar
  • How to identify all columns that contain binary representation
  • Filter different groups by different factor levels
  • Saving .xlsx file to disc, form http post request
  • Add an "all" option under the filter that selects the number of rows displayed in a datatable
  • How to select second column of every xts in list
  • Generate a frequency dataframe out of an input dataframe
  • Why manual autocorrelation does not match acf() results?
  • Merge 3 dataframes which are different to each other
  • remove adjacent duplicates from string
  • How to change the position of stacked stacked bar chart in ggplot in R?
  • How to divide each of a range a variables by a second range of variables in R
  • Why do I need to assemble vector before scaling in Spark?
  • How to select individuals which appear in multiple groups?
  • How can I fill columns based on values in another column?
  • 32 bit R and 64 bit R: output differs
  • Remove a single backslash in paste0 output
  • ggplot2 different label for the first break
  • TSP in R, with given distances
  • How to find the given value from the range of values?
  • Solution on R group by issue _ multiple combination
  • Transform multiple columns with a function that uses different arguments per column
  • How can I parse a string with the format "1/16/2019 1:24:51" into a POSIXct or other date variable?
  • How to plot a box plot in R for outlier detection for a huge number of rows?
  • How to change column name according to another dataframe in R?
  • `sjPlot::tab_df()`--how to set the number of decimal places?
  • time average for specific time range in r
  • joining dataframes by closest time and another key in r
  • How to create nested for loop for a certain range
  • New category based on sequence of date ranges
  • how to extract formula from coxph model summary in R?
  • add row based on variable condition in R
  • Generating the sequence 111122222333334
  • Unable to use has_goog_key() in R
  • how to multiply each row with a scaler in corresponding column?
  • R is not recognizing levels of a factor as the same. Is there a way to do this?
  • Calculating mean of replicate experiment result values in a column based on multiple columns using R
  • Best method to extract the first instance of a string between specified keywords using data.table
  • ignore optional combination of alphanumeric characters in str_extract
  • Why tracemem shows two copies when modification occurs inside function body?
  • Can't use mppm on multitype point patterns
  • How to move selected matrix rows to top of matrix based on a selection vector of row names
  • Combining expressions with a common operator
  • Passing string through multiple filters for matching
  • Convert two columns in R to rows of unique occurrence
  • How to create a dataframe using a function based on user-input?
  • How to access the visited vertices in a given shortest path using R igraph
  • Differences in Unicode character output with print()
  • Extracting Function or Objects from a String and then Piping Them with Magrittr/Dplyr
  • renderUI not evaluated until it is rendered
  • Find the maximum absolute value by row in an R data frame
  • Extracting data from irregular lists using purrr:map()
  • transforming data based on range of column in r
  • Identify and subset rows with some similar information
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com