如何为一列中的每个值复制行
假设我们有这样的列:
---------------------
record time id
---------------------
1 12:00 [1,2,3]
2 12:01 [4,5,6,7]
3 12:07 [8,9]
我想得到这样的结果:
---------------------
record time id
---------------------
1 12:00 1
2 12:00 2
3 12:00 3
4 12:01 4
5 12:01 5
...
9 12:07 9
我需要在 Postgresql 或 R 中执行此操作
最佳答案
如果“id”是string
,一个选项是separate_rows
library(tidyverse)
df1 %>%
separate_rows(id) %>%
filter(id != "") %>%
mutate(record = row_number())
# record time id
#1 1 12:00 1
#2 2 12:00 2
#3 3 12:00 3
#4 4 12:01 4
#5 5 12:01 5
#6 6 12:01 6
#7 7 12:01 7
#8 8 12:07 8
#9 9 12:07 9
如果'id'是一个列表
df1 %>%
unnest
数据
df1 <- structure(list(record = 1:3, time = c("12:00", "12:01", "12:07"
), id = c("[1,2,3]", "[4,5,6,7]", "[8,9]")), class = "data.frame",
row.names = c(NA, -3L))
关于sql - 如何为一列中的每个值复制相同的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54651958/