Related
Let´s say that my data has the following structure:
structure(list(Year = c(2000, 2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2001, 2001, 2001, 2001), Month = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Day = c(1,
1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 1, 1,
1, 1), FivMin = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3,
4, 1, 2, 3, 4, 1, 2, 3, 4), A = c(1, 2, 3, 0, 1, 5, 3, 4, 1,
0, 3, 1, 0, 2, 3, 0, 1, 2, 0, 9, 1, 2, 3, 0), B = c(2, 3, 4,
1, 2, 3, 0, 1, 2, 1, 4, -2, 2, 1, 0, 2, 2, 3, -1, 1, 2, 3, 4,
1), C = c(3, 0, 1, 2, 3, 4, 1, 9, 3, 7, 1, 2, 3, 4, 1, 2, 3,
4, 1, 2, 3, 0, 1, 2), D = c(4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3)), row.names = c(NA, -24L
), class = c("tbl_df", "tbl", "data.frame"))
My idea is use the crossproduct comand every day. In order to do that I wrote the following code:
res <- lapply(split(data, data[c("Year","Month","Day")]),
function(x) tcrossprod(t(x[c("A","B","C","D")])))
Final<-do.call(rbind, lapply(res, diag))
The output of Final is:
A B C D
2000.1.1 14 30 14 30
2001.1.1 14 30 14 30
2000.1.2 51 14 107 30
2001.1.2 0 0 0 0
2000.1.3 11 25 63 30
2001.1.3 0 0 0 0
2000.1.4 13 9 30 30
2001.1.4 0 0 0 0
2000.1.5 86 15 30 30
2001.1.5 0 0 0 0
What I need is a time serie (matrix or df object) formed by the diagonals calculated with crossproduct, It means my desired time serie would be
A B C D
2000.1.1 14 30 14 30
2000.1.2 51 14 107 30
2000.1.3 11 25 63 30
2000.1.4 13 9 30 30
2000.1.5 86 15 30 30
2001.1.1 14 30 14 30
What would be the changes in my original code. I think that i could replace the split command by grouped_by but it did not work.
As the split makes data frame into list, it creates 0 rows as well. Just remove those zero rows and try.
ls<- split(data, data[c("Year","Month","Day")])
ls<- ls[sapply(ls, nrow)>0]
res <- lapply(ls, function(x) tcrossprod(t(x[c("A","B","C","D")])))
Final<-do.call(rbind, lapply(res, diag))
Final <- Final[ order(row.names(Final)), ]
Final
Output:
A B C D
2000.1.1 14 30 14 30
2000.1.2 51 14 107 30
2000.1.3 11 25 63 30
2000.1.4 13 9 30 30
2000.1.5 86 15 30 30
2001.1.1 14 30 14 30
I have a data frame with 3 months of data with individual information. Individual information must be fixed during the whole period, however, in my real data set it is not the case. I would like to check row by row and highlight the dates that something went wrong during data entry.
Here is sample of my dataset ( real dataset has more variables):
input <- data.frame(stringsAsFactors=FALSE,
date = c(20190218, 20190219, 20190220, 20190221, 20190222,
20190223, 20190101, 20190103, 20190105, 20190110,
20190112, 20190218, 20190219, 20190220, 20190221, 20190222,
20190223),
id = c("18105265-ab", "18105265-ab", "18105265-ab",
"18105265-ab", "18105265-ab", "18105265-ab",
"18161665-aa", "18161665-aa", "18161665-aa", "18161665-aa",
"18161665-aa", "18502020-aa", "18502020-aa", "18502020-aa",
"18502020-aa", "18502020-aa", "18502020-aa"),
size = c(3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1),
type = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 2, 2),
county = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 5, 5, 5),
member_p10 = c(3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1),
youngest_age = c(5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7),
sex = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1),
position = c(5, 5, 5, 5, 5, 5, 4, 4, 4, 0, 0, 3, 3, 3, 3, 0, 0))
Is there any way for this type of operation? I would like to have this output at the end:
date id size type county member_p10 youngest_age sex position
1 20190221 18105265-ab 3 4 1 3 5 1 5
2 20190222 18105265-ab 2 4 1 2 7 1 5
3 20190105 18161665-aa 2 4 1 2 7 2 4
4 20190110 18161665-aa 1 2 1 1 7 2 0
5 20190221 18502020-aa 2 4 5 2 7 1 3
6 20190222 18502020-aa 1 2 5 1 7 1 0
I'm here again to ask for your help!
I'm trying to figure out what's happening with mutate_all (or with me...).
Let's say I have this dataset:
ds <- structure(list(Q1 = structure(c(5, 4, 5, 5, 5, 5, 5, 5, 5, 5,
5, 4, 3, 5, 5, 5, 5, 5, 1, 4, 5, 5, 3, 4, 5, 5, 5, 5, 5, 2, 5,
5, 4, 5, 5, 3, 5, 5, 4, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4,
5, 4), label = "1 Para mim é igual se os meus amigos são heterossexuais ou homossexuais.", format.spss = "F1.0", display_width = 3L, class = "labelled", labels = c(`discordo totalmente` = 1,
discordo = 2, indiferente = 3, concordo = 4, `concordo totalmente` = 5
)), Q2 = structure(c(1, 1, 1, 1, 1, 1, 3, 1, 2, 3, 1, 4, 4, 4,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2), label = "A homossexualidade é uma perturbação psicológica/biológica.", format.spss = "F1.0", display_width = 5L, class = "labelled", labels = c(`discordo totalmente` = 1,
discordo = 2, indiferente = 3, concordo = 4, `concordo totalmente` = 5
)), Q3 = structure(c(5, 2, 5, 4, 5, 4, 5, 5, 5, 4, 5, 5, 2, 3,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 4, 5, 4), label = "Acredito que os pais e as mães homossexuais são tão competentes como os pais e mães heterossexuais.", format.spss = "F1.0", display_width = 5L, class = "labelled", labels = c(`discordo totalmente` = 1,
discordo = 2, indiferente = 3, concordo = 4, `concordo totalmente` = 5
)), Q4 = structure(c(1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 2,
1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 5, 1, 1, 2, 1, 3), label = "4 Todas as Lésbicas, Gays, Bissexuais, Transexuais, Transgêneros e Intersexuais (LGBTI) me deixam irritado.", format.spss = "F1.0", display_width = 4L, class = "labelled", labels = c(`discordo totalmente` = 1,
discordo = 2, indiferente = 3, concordo = 4, `concordo totalmente` = 5
)), Q5 = structure(c(1, 4, 1, 1, 1, 1, 3, 1, 2, 1, 1, 1, 3, 3,
1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3, 2,
1, 1, 1, 2, 2, 5, 1, 4, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 3), label = "A legalização do casamento entre pessoas do mesmo sexo é muito errada.", format.spss = "F1.0", display_width = 5L, class = "labelled", labels = c(`discordo totalmente` = 1,
discordo = 2, indiferente = 3, concordo = 4, `concordo totalmente` = 5
))), row.names = c(NA, -54L), class = c("tbl_df", "tbl", "data.frame"
))
Then I need to transform all variables into factors to plot them. I really like the dplyr approach:
ds_mutate <- ds %>% mutate_all(., factor, levels=1:5)
likert(ds_mutate)
But this error is coming up:
Error in likert(ds_mutate) :
All items (columns) must have the same number of levels
When I use lapply (Nobody will convince me 'apply'functions are intuitive...), it works pretty well:
> ds_apply <- lapply(ds, factor, levels=1:5) %>% as.data.frame()
> likert(ds_apply)
Item 1 2 3 4 5
1 Q1 1.851852 1.851852 9.259259 14.814815 72.222222
2 Q2 77.777778 9.259259 5.555556 7.407407 0.000000
3 Q3 0.000000 3.703704 1.851852 14.814815 79.629630
4 Q4 79.629630 14.814815 3.703704 0.000000 1.851852
5 Q5 72.222222 7.407407 14.814815 3.703704 1.851852
But as you can see, the str is (for me) the same...
i'm looking forward to hearing from you!!
Thank you!
There is one difference:
class(ds_mutate)
# [1] "tbl_df" "tbl" "data.frame"
class(ds_apply)
# [1] "data.frame"
The issue then arises from the fact that, in the call of likert, we have
nlevels = length(levels(items[, 1]))
where, in the former case,
length(levels(ds_mutate[, 1]))
# [1] 0
since
ds_mutate[, 1]
# A tibble: 54 x 1
# Q1
# <fct>
# 1 5
# 2 4
# 3 5
# 4 5
# 5 5
# 6 5
# 7 5
# 8 5
# 9 5
# 10 5
# … with 44 more rows
i.e., the result is a tibble. Also,
methods("levels")
# [1] levels.default
so that there is no levels method for tibbles. Notice also that
class(ds_mutate) <- c("data.frame", "tbl_df", "tbl")
ds_mutate[, 1]
# [1] 5 4 5 5 5 5 5 5 5 5 5 4 3 5 5 5 5 5 1 4 5 5 3 4 5 5 5 5 5 2 5 5 4 5 5 3 5 5 4 3 3 5 5 5
# [45] 5 5 5 5 5 5 5 4 5 4
# Levels: 1 2 3 4 5
in which case
likert(ds_mutate)
starts to work too. Without modifying classes you may also use
likert(data.frame(ds_mutate))
Extra: lapply in
lapply(ds, factor, levels = 1:5)
actually is really intuitive once we understand one thing: a data frame is a special case of a list where each list element is of the same length. Know the way sapply or lapply works is that it goes over each element of the first argument: once we see ds as a data frame whose elements (since it's a list) are columns, it becomes clear how it operates. For the same reason, since the results of factor in this case are of the same length, the list resulting from the call to lapply nicely can be converted to a data frame.
I never used likert package but it looks like it doesn't take an object of the class tibble. This works for me:
likert(as.data.frame(ds_mutate))
I am learning R (focused on the tidyverse packages) and am hoping that someone could help with the following problem that has me stumped.
I have a data-set that looks similar to the following:
library("tibble")
myData <- frame_data(
~id, ~r1, ~r2, ~r3, ~r4, ~r5, ~r6, ~r7, ~r8, ~r9, ~r10, ~r11, ~r12, ~r13, ~r14, ~r15, ~r16,
"A", 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
"B", 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
"C", 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2,
"D", 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1, 1, 2, 2, 2,
"E", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
)
Basically, I have multiple rows of respondent data, and each respondent gave 16 responses of either "1" or "2".
For each respondent (i.e., each row) I would like to create an additional three columns:
The first new column - called "switchCount" - identifies the number of times the respondent switched from a "2" response to a "1" response.
The second new column - called "switch1" - identifies the index of the first time the respondent switched from a "2" response to a "1" response.
The third new column - called "switch2" - identifies the index of the final time the respondent switched from a "2" response to a "1" response.
If there is no switch and all values are "2", then return the index of 0.
If there is no switch and all values are "1", then return the index of 16.
The final datatable should therefore look like this:
myData <- frame_data(
~id, ~r1, ~r2, ~r3, ~r4, ~r5, ~r6, ~r7, ~r8, ~r9, ~r10, ~r11, ~r12, ~r13, ~r14, ~r15, ~r16, ~switchCount, ~switch1, ~switch2,
"A", 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 1,
"B", 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4,
"C", 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 3, 9,
"D", 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 3, 6, 15,
"E", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 16, 16
)
One approach could be to concatenate all response columns row wise and then find the occurrences of 2,1 using gregexpr
library(dplyr)
myData %>%
rowwise() %>%
mutate(concat_cols = paste(r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,sep=";"),
switchCount = ifelse(gregexpr("2;1", concat_cols)[[1]][1] == -1,
0,
length(gregexpr("2;1", concat_cols)[[1]])),
switch1 = ifelse(switchCount == 0,
ifelse(grepl("2",concat_cols), 1, 16),
min(floor(gregexpr("2;1", concat_cols)[[1]]/2)+1)),
switch2 = ifelse(switchCount == 0,
ifelse(grepl("2",concat_cols), 1, 16),
max(floor(gregexpr("2;1", concat_cols)[[1]]/2)+1))) %>%
select(-concat_cols)
Output is:
id r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16 switchCount switch1 switch2
1 A 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 1 1
2 B 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4
3 C 2 2 2 1 1 1 2 2 2 1 1 1 1 2 2 2 2 3 9
4 D 1 1 2 2 2 2 1 1 2 2 1 1 1 2 2 1 3 6 15
5 E 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 16 16
Sample data:
myData <- structure(list(id = c("A", "B", "C", "D", "E"), r1 = c(2, 2,
2, 1, 1), r2 = c(2, 2, 2, 1, 1), r3 = c(2, 2, 2, 2, 1), r4 = c(2,
2, 1, 2, 1), r5 = c(2, 1, 1, 2, 1), r6 = c(2, 1, 1, 2, 1), r7 = c(2,
1, 2, 1, 1), r8 = c(2, 1, 2, 1, 1), r9 = c(2, 1, 2, 2, 1), r10 = c(2,
1, 1, 2, 1), r11 = c(2, 1, 1, 1, 1), r12 = c(2, 1, 1, 1, 1),
r13 = c(2, 1, 1, 1, 1), r14 = c(2, 1, 2, 2, 1), r15 = c(2,
1, 2, 2, 1), r16 = c(2, 1, 2, 1, 1), switchCount = c(0, 1,
2, 3, 0), switch1 = c(1, 4, 3, 6, 16), switch2 = c(1, 4,
9, 15, 16)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
My data looks like this:
> 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")
It is organized by unique orgnationationunitname, but the columns are also grouped into different categories. For e.g. cd4, eid, vl, hiv, tbafb etc. and then into perform, participate & pass_result. I want to tabulate the data across all these categories to look like this
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
How can I do this in R, without having to create separate data frames?