Expanding rows of data

1.7k views Asked by At

I have an issue of expanding rows of my data frame. I tried expand from tidyr inside of a dplyr chain. The point is that it seems that this function is expanding the data but by changing the order of expand element which is not desired. I want to keep order of sp column after expand.

Here is my attempt

df <- data.frame(label1=letters[1:6],label2=letters[7:12])

sp <- c(-1,0,seq(0.1,0.5,0.1),seq(-2,-2.5,-0.1),seq(0.1,0.5,0.1))
sp
#     [1] -1.0  0.0  0.1  0.2  0.3  0.4  0.5 -2.0 -2.1 -2.2 -2.3 -2.4 -2.5  0.1  0.2  0.3  0.4  0.5


library(dplyr)
library(tidyr)

    expanded <- df%>%
      expand(df,sp)

> head(expanded)
  label1 label2   sp
1      a      g -2.5
2      a      g -2.4
3      a      g -2.3
4      a      g -2.2
5      a      g -2.1
6      a      g -2.0

I want to expand df based on sp order. how can we do that?

expected output

label1 label2   sp
1       a      g -1.0
2       a      g  0.0
3       a      g  0.1
4       a      g  0.2
5       a      g  0.3
6       a      g  0.4
7       a      g  0.5
8       a      g  -2
9       a      g  -2.1
10      a      g  -2.2
11      a      g  -2.3
12      a      g  -2.4
13      a      g  -2.5
14      b      h  -1.0
15      b      h   0.0
16      b      h   0.1

and so on

1

There are 1 answers

11
akrun On BEST ANSWER

We can match the column 'sp' with the vector sp in the global environment to do the ordering

r1 <- df %>%
         expand(df, sp) %>%
         arrange(label1, label2, match(sp, unique(.GlobalEnv$sp)))
dim(r1)
#[1] 78  3


identical(unique(r1$sp), unique(sp))
#[1] TRUE

Update

If there are duplicates in the 'sp' vector and we want to expand on all the values, one option is to do the expansion on the sequence of the vector and later change the values

r2 <- df %>%
        expand(df, sp=seq_along(sp)) %>% 
        mutate(sp = .GlobalEnv$sp[sp])


dim(r2)
#[1] 108   3

head(r2, length(sp))
#    label1 label2   sp
# 1       a      g -1.0
# 2       a      g  0.0
# 3       a      g  0.1
# 4       a      g  0.2
# 5       a      g  0.3
# 6       a      g  0.4
# 7       a      g  0.5
# 8       a      g -2.0
# 9       a      g -2.1
# 10      a      g -2.2
# 11      a      g -2.3
# 12      a      g -2.4
# 13      a      g -2.5
# 14      a      g  0.1
# 15      a      g  0.2
# 16      a      g  0.3
# 17      a      g  0.4
# 18      a      g  0.5