简体   繁体   中英

How to transform/resample/interpolate data for normalising variable length within a tidy dataset with multiple grouping variables in R?

I am aiming to normalize the length of vectors for averaging within a tidy data set. Using approx seems to be way to go but I can't make it work efficiently in tidyverse. One issue is probably related to resizing within a dataframe. Here's a reproducible example:

# create reproducible dataset
i = 80
I = 110
id = rep("AA", I+i)
event = rep("event1", I+i)
sub_event = NA
sub_event[1:i] = 1
sub_event[i+1:I] = 2
sub_event = as.factor(sub_event)
y1 = sin(seq(0, 5*pi, length.out = i))
y2 = sin(seq(0, 5*pi, length.out = I))
y3 = cos(seq(0, 5*pi, length.out = i))
y4 = cos(seq(0, 5*pi, length.out = I))
var1 = c(y1,y2)
var2 = c(y3,y4)

df1 <- data.frame(id, event, sub_event,var1, var2)
df2 <- df1
df2$event = "event2"
df <- rbind(df1, df2)
temp <- df
temp$id = "BB"
df <- rbind(df, temp)


# create a "time" vector for sub_event

df <- df %>% 
  group_by(id, event, sub_event) %>%
  mutate(sub_event_time = seq_along(var1)) %>%
  select(id, event, sub_event, sub_event_time, everything()) %>%
  ungroup()

Plot var1

# plot 
ggplot(df,
       aes(x=sub_event_time, y=var1, colour = sub_event)) + 
  geom_point() +
  geom_path() +
  facet_wrap(id~event)

在此处输入图片说明

I want transform/resample data to obtain length of var1 for each sub_events to be the length of the longest sub_event within each event for each id.

For instance we want: length of var1 for event 1 sub event 1 = length of var1 for event 1 sub event 2 (which is the longest).

Here's an attempt:

# attempt for var1 only
aim.df <- df %>%
  ungroup() %>%
  select(-var2) %>%
  group_by(id, event) %>%
  mutate(max_sub_event_time = max(sub_event_time)) %>%
  mutate(var1 = approx(var1, n = max_sub_event_time)$y) 

This returns the following error:

    Error in mutate_impl(.data, dots) : 
    Column `var1` must be length 190 (the group size) or one, not 110
    In addition: Warning messages:
    1: In if (n <= 0) stop("'approx' requires n >= 1") :
    the condition has length > 1 and only the first element will be used
    2: In seq.int(x[1L], x[nx], length.out = n) :
    first element used of 'length.out' argument

Any ideas ?

steps...

  1. group_by(id, event, sub_event)
  2. remove sub_event_time since it will be irrelevant once you add observations
  3. summarise the result of the approx function as a list column (you will have to convert var1 and max_sub_event_time to appropriate input for approx )
  4. unnest the resulting list column
  5. group_by(id, event, sub_event) again and add a new sub_event_time

code...

library(dplyr)
library(tidyr)

df %>%
  ungroup() %>%
  select(-var2) %>%
  group_by(id, event) %>%
  mutate(max_sub_event_time = max(sub_event_time)) %>% 
  group_by(id, event, sub_event) %>% 
  select(-sub_event_time) %>% 
  summarise(var1_int = list(approx(as.numeric(var1), n = first(max_sub_event_time))$y)) %>% 
  unnest() %>% 
  group_by(id, event, sub_event) %>% 
  mutate(sub_event_time = row_number())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM