我想将data.frame all
拆分为基于value
列的唯一行:
all
数据框:
seq start value
ch1 20 "NE001", "NE002"
ch2 30 "NE002", "NE003"
ch3 40 "NE001", "NE002", "NE003"
我的预期输出:
seq start value
ch1 20 NE001
ch1 20 NE002
ch2 30 NE002
ch2 30 NE003
ch3 40 NE001
ch3 40 NE002
ch3 40 NE003
有什么想法吗?谢谢!
答案 0 :(得分:2)
这是实现它的一种略微繁琐的方式。
d <- data.frame(seq=c('ch1', 'ch2', 'ch3'),
start=c(20, 30, 40),
value=c('"NE001", "NE002"',
'"NE002", "NE003"',
'"NE001", "NE002", "NE003"'))
# sub out quotes and split values into vectors
v <- strsplit(gsub('"', '', d$value), ', ')
cbind(d[rep(seq_along(v), sapply(v, length)), 1:2],
value=unlist(v))
# seq start value
# 1 ch1 20 NE001
# 1.1 ch1 20 NE002
# 2 ch2 30 NE002
# 2.1 ch2 30 NE003
# 3 ch3 40 NE001
# 3.1 ch3 40 NE002
# 3.2 ch3 40 NE003
根据需要删除row.names。