We could extract the characters that are not a ]
that succeeds the [
with str_extract_all
, which returns a list
, then unnest
the columns, and split the column values at the ,
followed by one or more spaces (\s+
) with separate_rows
library(dplyr)
library(stringr)
library(tidyr)
df %>%
mutate(across(everything(), str_extract_all, "(?<=\[)[^]]+")) %>%
unnest(c(NDVIs, dates)) %>%
separate_rows(c(NDVIs, dates), sep=",\s+", convert = TRUE)
-output
# A tibble: 198 x 2
# NDVIs dates
# <dbl> <dbl>
# 1 0.00485 1527502095000
# 2 0.0319 1544955875000
# 3 0.0347 1544955875000
# 4 0.0569 1545126378000
# 5 0.0596 1545126378000
# 6 0.0500 1545394367000
# 7 0.0523 1545394367000
# 8 0.0525 1545561339000
# 9 0.0552 1545561339000
#10 -0.0102 1545820928000
# … with 188 more rows
Or with the []
is not in every element, then remove the already existing one with str_remove_all
and use separate_rows
df %>%
mutate(across(everything(), str_remove_all, "\[|\]")) %>%
separate_rows(c(NDVIs, dates), sep=",\s+", convert = TRUE)
# A tibble: 198 x 2
# NDVIs dates
# <dbl> <dbl>
# 1 0.00485 1527502095000
# 2 0.0319 1544955875000
# 3 0.0347 1544955875000
# 4 0.0569 1545126378000
# 5 0.0596 1545126378000
# 6 0.0500 1545394367000
# 7 0.0523 1545394367000
# 8 0.0525 1545561339000
# 9 0.0552 1545561339000
#10 -0.0102 1545820928000
# … with 188 more rows
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…