I have the following data structure
ID Type Values
1 A 5; 7; 8
2 A 6
3 B 2; 3
and I would like to reshape it to the fol
The answers so far are great. Here's yet another.
# The data
DF <- data.frame(ID=1:3,
Type=c('A','A','B'),
Values=c(' 5; 7; 8', '6', ' 2;3'))
This solution uses the colsplit() function from the "reshape2" package. One downside is that it expects you to know the number of resulting columns needed.
require(reshape2)
DF2 <- data.frame(DF[-3], colsplit(DF$Values, ";", c("V.1", "V.2", "V.3")))
na.omit(melt(DF2, id.vars=c("ID", "Type")))
# ID Type variable value
# 1 1 A V.1 5
# 2 2 A V.1 6
# 3 3 B V.1 2
# 4 1 A V.2 7
# 6 3 B V.2 3
# 7 1 A V.3 8
From here you can sort and drop columns as required to get your final desired output.