r - How to update a frequency table based on a third variable? -
I have a data frame that shows different class views of three years (2006,2007,2008) Number of Incident:
df < -data.frame (count = c (150,33,35,26,15,65), Y.2006 = c ("one", "a", "a", "a", "d", "d "," D "), Y. 2007 = C (" A "," B "," B "," C "," C "" C "), Y.2008 = c (" a "," b " , "A", "c", "c", "d")) looks like this:
count Y.2006 Y.2007 Y.2008 1 150 AAA 2 33 ABB 3 35 ABA 4 26 DCC 5 15 DCC 6 65DCD I calculate the sequence of 2006-2007:
df $ Y.2006_2007 & lt; -paste (df $ Y.2006, df $ Y.2007) Result:
Count Y.2006 Y.2007 Y.2008 Y. 2006_2007 1 150 aha 2 33 abbab 3 35 abaab 4 26 dccdc 5 15 dccdc 6 65 dcddc I want the number of incident for Y.2006_2007 for different classes of 2008 Therefore I do:
Table (df $ Y.2006_2007, df $ Y 2008) results in
abcdaa 1 0 0 AB 1 1 0 DC 0 0 2 1 Now, I would like the initial data frame (DF) to keep in mind the number of event of class sequences ) I would like to update these results on the basis of counting variables:
abcdaa 150 0 0 AB 35 33 0 DC 0 0 41 65 < P> I do not know how to get this result any idea?
You can use xtabs so that the base R: xtabs (formula = calculation ~ Y.2006_2007 + Y.2008, data = DF) Y.2008 Y.2006_2007 abcdaa 150 0 0 AB 35 33 0 DC 0 0 41 65 and if you need data, you can wrap it in as.data.frame : as .data.frame (xtabs (formula = calculation ~ Y.2006_2007 + Y.2008, data = DF)) Y.2006_2007 Y.2008 Freq 1 aaa 150 2 ABA 35 3 DCA 0 4AAB 0 5 ABB 33 6DCB 0 7 AAC 0 ABC09DCC 41 10AAD 011 ABD 0 12DCD 65
Comments
Post a Comment