[R] Correcting for missing data combinations
Charles C. Berry
cberry at tajo.ucsd.edu
Fri Dec 11 22:34:14 CET 2009
On Fri, 11 Dec 2009, GL wrote:
>
> I can think of many brute-force ways to do this outside of R, but was
> wondering if there was a simple/elegant solution within R instead.
>
> I have a table that looks something like the following:
>
> Factor1 Factor2 Value
> A 11/11/2009 5
> A 11/12/2009 4
> B 11/11/2009 7
> B 11/13/2009 8
>
>> From that I need to generate all permutations of Factor1 and Factor2 and
> force a 0 for any combination that doesn’t exist in the actual data table.
> By way of example, I’d like the output for above to end up as:
>
> Factor1 Factor2 Value
> A 11/11/2009 5
> A 11/12/2009 4
> A 11/13/2009 0
> B 11/11/2009 7
> B 11/12/2009 0
> B 11/13/2009 8
>
> Truly appreciate any thoughts.
# copy the 'table' to the clipboard, then:
> dat <- read.table("clipboard",header=T)
> res <- as.data.frame(xtabs(Value~., dat ))
> colnames(res) <- sub("Freq","Value",colnames(res))
> res
Factor1 Factor2 Value
1 A 11/11/2009 5
2 B 11/11/2009 7
3 A 11/12/2009 4
4 B 11/12/2009 0
5 A 11/13/2009 0
6 B 11/13/2009 8
>
HTH,
Chuck
>
> --
> View this message in context: http://n4.nabble.com/Correcting-for-missing-data-combinations-tp961301p961301.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list