I'm collecting logs in R with 3 columns:
week, probe, and number of observations.
There aren't record when there is no observation.
week=c(1,2,2,4)
probe=c("A","C","B","C")
obs=c(2,4,3,1)
logs=data.frame(week,probe,obs)
logs
week probe obs
1 A 2
2 C 4
2 B 3
4 C 1
I want to reformat the data so that it includes all weeks and all probes even if there was no observation, so that it looks like this:
week probe obs
1 A 2
1 B 0
1 C 0
1 D 0
2 A 0
2 B 0
2 C 3
2 D 4
3 A 0
3 B 0
3 C 0
3 D 0
4 A 0
4 B 0
4 C 1
4 D 0
I have the list of all probes here:
allprobes=c("A","B","C","D")
and I want to look at these weeks:
allweeks=c(1:4)
I've been looking at melt, cast, reshape, but I only manage to get 1 line per id or month...
as I actually want to keep the original format of the logs.
It seems easy enough at first but I'm now stuck...
Any advice on how to get the data formatted this way?
Thanks a lot for any help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…