Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

tidyr - Transforming a column of arrays to column of single values in R

I have two columns of arrays that I would like to give individuals cells to. For example, my columns currently look like this:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...