如何在R中创建嵌套数据 - 嵌套不同类别的多个列

时间:2016-11-17 17:48:49

标签: r matrix multidimensional-array dataframe panel-data

我的数据如下:



> str(tab2)
'data.frame':	36 obs. of  22 variables:
 $ organisationunitname        : Factor w/ 38 levels "All OUs","Angola",..: 2 3 4 5 6 7 8 9 10 11 ...
 $ cd4_perform_result          : num  24 6 7 1 1 1 5 3 2 1 ...
 $ cd4_participate_result      : num  1 8 8 1 1 1 5 3 2 1 ...
 $ cd4_pass_result             : num  1 4 19 1 1 1 5 3 2 1 ...
 $ eid_perform_result          : num  2 1 7 1 1 1 1 9 1 1 ...
 $ eid_participate_result      : num  2 1 5 1 1 1 1 8 1 1 ...
 $ eid_pass_result             : num  2 1 5 1 1 1 1 7 1 1 ...
 $ vl_perform_result           : num  2 1 3 1 1 1 1 9 1 1 ...
 $ vl_participate_result       : num  2 1 7 1 1 1 1 7 1 1 ...
 $ vl_pass_result              : num  2 1 7 1 1 1 1 7 1 1 ...
 $ hiv_perform_result          : num  19 29 14 1 1 1 26 21 10 1 ...
 $ hiv_participate_result      : num  19 28 14 1 1 1 22 20 4 1 ...
 $ hiv_pass_result             : num  20 28 14 1 1 1 18 22 7 1 ...
 $ tbafb_perform_result        : num  9 1 8 1 1 1 1 7 1 1 ...
 $ tbafb_participate_result    : num  1 1 18 1 1 1 1 5 1 1 ...
 $ tbafb_pass_result           : num  1 1 19 1 1 1 1 6 1 1 ...
 $ tbculture_perform_result    : num  3 1 2 1 1 1 1 1 1 1 ...
 $ tbculture_participate_result: num  1 1 2 1 1 1 1 1 1 1 ...
 $ tbculture_pass_result       : num  1 1 1 1 1 1 1 1 1 1 ...
 $ tbxpert_perform_result      : num  1 1 4 1 1 1 1 1 1 1 ...
 $ tbxpert_participate_result  : num  1 1 5 1 1 1 1 1 1 1 ...
 $ tbxpert_pass_result         : num  1 1 2 1 1 1 1 1 1 1 ...
> 




DATA



structure(list(country = c("eRkf", "KJfd", "wjkO", "Hovb", "v6Dm", 
"vp8p", "TYhI", "U4OB", "GVnL", "dzJO", "11JX", "ygWc", "4Ye1", 
"RykQ", "OHLW", "Xh1x", "MOl4", "67vY", "h2cA", "Ue1r", "Hr9G", 
"YxpI", "S0Or", "2fss", "wz9F", "XEOG", "Vptm", "xAup", "STBG", 
"AayU", "mJyW", "PvNG", "qncq", "L8dk", "6CJ8", "90i7"), cd4_perform_result = c(23, 
6, 7, 1, 1, 1, 5, 3, 2, 1, 10, 1, 2, 8, 1, 2, 16, 1, 1, 22, 12, 
1, 13, 11, 17, 1, 20, 15, 1, 21, 18, 4, 1, 14, 19, 9), cd4_participate_result = c(1, 
8, 8, 1, 1, 1, 5, 3, 2, 1, 7, 1, 2, 9, 1, 2, 16, 1, 1, 17, 11, 
1, 4, 14, 13, 1, 19, 15, 1, 20, 10, 6, 1, 18, 12, 3), cd4_pass_result = c(1, 
4, 18, 1, 1, 1, 5, 3, 2, 1, 20, 1, 2, 19, 1, 2, 12, 1, 1, 13, 
6, 1, 10, 11, 9, 1, 17, 2, 1, 15, 7, 16, 1, 14, 8, 3), eid_perform_result = c(2, 
1, 6, 1, 1, 1, 1, 8, 1, 1, 7, 1, 2, 3, 5, 2, 5, 1, 1, 9, 5, 1, 
4, 2, 10, 1, 5, 1, 1, 5, 8, 2, 1, 1, 8, 5), eid_participate_result = c(2, 
1, 4, 1, 1, 1, 1, 7, 1, 1, 6, 1, 2, 9, 4, 2, 4, 1, 1, 3, 2, 1, 
9, 2, 8, 1, 4, 1, 1, 4, 6, 2, 1, 1, 5, 4), eid_pass_result = c(2, 
1, 4, 1, 1, 1, 1, 6, 1, 1, 5, 1, 2, 9, 1, 2, 4, 1, 1, 3, 2, 1, 
8, 2, 7, 1, 4, 1, 1, 4, 5, 2, 1, 1, 4, 4), vl_perform_result = c(2, 
1, 3, 1, 1, 1, 1, 8, 1, 1, 9, 1, 2, 10, 4, 2, 4, 1, 1, 5, 4, 
1, 7, 6, 5, 1, 11, 1, 1, 4, 8, 2, 1, 1, 7, 4), vl_participate_result = c(2, 
1, 7, 1, 1, 1, 1, 7, 1, 1, 8, 1, 2, 8, 4, 2, 4, 1, 1, 5, 2, 1, 
4, 6, 3, 1, 9, 1, 1, 4, 7, 2, 1, 1, 6, 1), vl_pass_result = c(2, 
1, 7, 1, 1, 1, 1, 7, 1, 1, 9, 1, 2, 8, 1, 2, 5, 1, 1, 4, 2, 1, 
2, 6, 3, 1, 10, 1, 1, 5, 7, 2, 1, 1, 5, 1), hiv_perform_result = c(18, 
28, 13, 1, 1, 1, 25, 20, 10, 1, 6, 11, 9, 7, 19, 26, 8, 14, 1, 
27, 12, 1, 24, 17, 23, 1, 21, 5, 1, 22, 16, 15, 1, 2, 3, 4), 
    hiv_participate_result = c(18, 27, 13, 1, 1, 1, 21, 19, 4, 
    1, 15, 9, 10, 3, 11, 26, 5, 1, 1, 20, 6, 1, 23, 17, 12, 1, 
    24, 8, 1, 22, 14, 16, 1, 2, 25, 7), hiv_pass_result = c(19, 
    27, 13, 1, 1, 1, 17, 21, 6, 1, 16, 26, 10, 2, 23, 25, 9, 
    1, 1, 14, 4, 1, 20, 18, 11, 1, 22, 7, 1, 15, 12, 8, 1, 3, 
    24, 5), tbafb_perform_result = c(9, 1, 8, 1, 1, 1, 1, 7, 
    1, 1, 6, 1, 20, 5, 1, 2, 12, 1, 1, 15, 13, 1, 17, 11, 19, 
    1, 10, 1, 1, 14, 16, 4, 1, 18, 3, 1), tbafb_participate_result = c(1, 
    1, 17, 1, 1, 1, 1, 5, 1, 1, 12, 1, 18, 11, 1, 2, 6, 1, 1, 
    13, 7, 1, 10, 9, 14, 1, 8, 1, 1, 16, 15, 4, 1, 17, 3, 1), 
    tbafb_pass_result = c(1, 1, 18, 1, 1, 1, 1, 6, 1, 1, 13, 
    1, 19, 11, 1, 2, 4, 1, 1, 15, 5, 1, 7, 10, 12, 1, 8, 1, 1, 
    16, 9, 3, 1, 14, 17, 1), tbculture_perform_result = c(3, 
    1, 2, 1, 1, 1, 1, 1, 1, 1, 6, 1, 3, 8, 1, 2, 2, 1, 1, 7, 
    3, 1, 5, 4, 7, 1, 5, 1, 1, 3, 6, 6, 1, 3, 3, 1), tbculture_participate_result = c(1, 
    1, 2, 1, 1, 1, 1, 1, 1, 1, 5, 1, 3, 8, 1, 2, 2, 1, 1, 7, 
    2, 1, 6, 4, 6, 1, 1, 1, 1, 3, 3, 5, 1, 3, 3, 1), tbculture_pass_result = c(1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 7, 1, 2, 2, 1, 1, 8, 
    2, 1, 6, 4, 5, 1, 1, 1, 1, 3, 3, 6, 1, 3, 3, 1), tbxpert_perform_result = c(1, 
    1, 4, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 16, 1, 8, 3, 1, 1, 5, 
    9, 1, 15, 7, 13, 1, 4, 1, 1, 12, 11, 1, 1, 6, 14, 10), tbxpert_participate_result = c(1, 
    1, 5, 1, 1, 1, 1, 1, 1, 1, 15, 1, 1, 4, 1, 12, 3, 1, 1, 2, 
    7, 1, 16, 9, 11, 1, 1, 1, 1, 14, 10, 1, 1, 6, 8, 13), tbxpert_pass_result = c(1, 
    1, 2, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 4, 1, 9, 3, 1, 1, 14, 
    6, 1, 13, 8, 8, 1, 1, 1, 1, 11, 6, 1, 1, 5, 7, 10)), .Names = c("country", 
"cd4_perform_result", "cd4_participate_result", "cd4_pass_result", 
"eid_perform_result", "eid_participate_result", "eid_pass_result", 
"vl_perform_result", "vl_participate_result", "vl_pass_result", 
"hiv_perform_result", "hiv_participate_result", "hiv_pass_result", 
"tbafb_perform_result", "tbafb_participate_result", "tbafb_pass_result", 
"tbculture_perform_result", "tbculture_participate_result", "tbculture_pass_result", 
"tbxpert_perform_result", "tbxpert_participate_result", "tbxpert_pass_result"
), row.names = c(NA, 36L), class = "data.frame")




它由唯一的orgnationationunitname组织,但列也分组为不同的类别。对于例如cd4,eid,vl,hiv,tbafb等然后进行表演,参与& pass_result。我想将所有这些类别的数据制成表格,如下所示



Country: eRkf
cat	         CD4	EID	VL	HIV 	TB AFB 	TB Culture	TB Xpert
Perform	        3442	288	114	29519	8572	72	        591
Participate	1771	128	95	17342	5433	119	        395
Pass_test	1535	118	83	11674	4508	109	        343




如何在R中执行此操作,而无需创建单独的数据框?

0 个答案:

没有答案