我有一个包含50种中介技术的数据框架,以及4种不同的分类.每个人都可以将一种技术分为多个类别.我想计算每次为特定类别(1、2、3或4) Select 一种技术时发生的次数,并用这些加起来创建一个新的数据帧.
当前数据帧的示例:
Techniques_1 | Techniques_2 | Techniques_3 | Techniques_4 | Techniques_5 | Techniques_6 |
---|---|---|---|---|---|
1,3 | 2 | 4 | 2,3 | 1 | 1 |
2,3 | 3 | 4 | 2,3 | 1 | 1 |
2 | 2 | 3 | 3 | 1,2 | 1,2,3 |
1 | 3 | 3 | 1 | 2 | 2 |
2 | 2,3 | 2,3 | 1,2,3 | 1 | 1,2 |
所需的新数据帧:
Category_Count | Technique_1 | Technique_2 | Technique_3 | Technique_4 | Technique_5 | Technique_6 |
---|---|---|---|---|---|---|
Category_1 | 2 | 0 | 0 | 2 | 4 | 4 |
Category_2 | 3 | 3 | 1 | 3 | 2 | 3 |
Categroy_3 | 2 | 3 | 3 | 4 | 0 | 1 |
Category_4 | 0 | 0 | 2 | 0 | 0 | 0 |
Here is my data set, the counts are different from my example as they
were made up. Had a go at using dput, hopefully this is correct:
Med_Tech_Structure <- structure(list(Techniques_1 = c("2", "2", "2",
NA, "1", "1"),
Techniques_2 = c("2", "2", "2", NA, "1,2", "2"), Techniques_3 =
c("2",
"2", "2", NA, "1", "1"), Techniques_4 = c("2", "2", "2",
NA, "1", "2"), Techniques_5 = c("2,3", "4", "3", NA, "4",
"1"), Techniques_6 = c("3", "3", "3", NA, "4", "3"), Techniques_7 =
c("2",
"2", "2", NA, "1", "1"), Techniques_8 = c("1", "4", "1,3",
NA, "4", "2"), Techniques_9 = c("2", "2", "2", NA, "2", "2"
), Techniques_10 = c("1", "4", "1", NA, "2", "1"), Techniques_11 =
c("2",
"4", "1,2", NA, "4", "4"), Techniques_12 = c("1", "4", "2",
NA, "4", "4"), Techniques_13 = c("2,3", "4", "1,2", NA, "4",
"4"), Techniques_14 = c("2", "4", "1,2", NA, "2", "4"), Techniques_15
= c("2",
"4", "2,3", NA, "4", "4"), Techniques_16 = c("1", "1", "3",
NA, "1", "1"), Techniques_17 = c("3", "3", "3", NA, "4",
"4"), Techniques_18 = c("2", "4", "3", NA, "1", "1"), Techniques_19 =
c("2",
"2", "2", NA, "2", "2"), Techniques_20 = c("2", "4", "1",
NA, "4", "4"), Techniques_21 = c("1,2", "4", "1,2", NA, "4",
"4"), Techniques_22 = c("1,2", "1", "1,2", NA, "2", "4"),
Techniques_23 = c("2", "4", "2", NA, "2", "4"), Techniques_24 =
c("2",
"2", "2", NA, "2", "2"), Techniques_25 = c("2", "4", "2",
NA, "4", "4"), Techniques_26 = c("1,2", "4", "1,3", NA, "4",
"3"), Techniques_27 = c("2", "4", "3", NA, "1", "1,2"), Techniques_28
= c("2",
"4", "2", NA, "3", "4"), Techniques_29 = c("1,2", "4", "1,2",
NA, "4", "4"), Techniques_30 = c("1,2", "4", "2", NA, "4",
"4"), Techniques_31 = c("2", "4", "2,3", NA, "1,2", "1"),
Techniques_32 = c("2,3", "4", "1,3", NA, "4", "4"), Techniques_33 =
c("1,2",
"4", "1,2", NA, "4", "4"), Techniques_34 = c("1,2", "4",
"2,3", NA, "4", "4"), Techniques_35 = c("1,2", "4", "2,3",
NA, "4", "4"), Techniques_36 = c("2", "4", "2,3", NA, "4",
"4"), Techniques_37 = c("1,2", "4", "2,3", NA, "4", "4"),
Techniques_38 = c("1", "1", "2", NA, "1", "1"), Techniques_39 =
c("1,2",
"4", "2", NA, "1", "4"), Techniques_40 = c("1,2", "4", "2",
NA, "4", "4"), Techniques_41 = c("1,2", "4", "2", NA, "4",
"4"), Techniques_42 = c("1,2", "4", "1,2", NA, "4", "4"),
Techniques_43 = c("1,2,3", "4", "1,2", NA, "4", "4"), Techniques_44 =
c("1,2,3",
"3", "1,2,3", NA, "4", "4"), Techniques_45 = c("1,2", "1",
"2,3", NA, "4", "4"), Techniques_46 = c("1", "4", "1,2",
NA, "1,2", "4"), Techniques_47 = c("2", "4", "2", NA, "4",
"4"), Techniques_48 = c("1", "2", "2", NA, "1", "2"), Techniques_49 =
c("2",
"4", "2", NA, "1", "4"), Techniques_50 = c("1,2", "4", "2,3",
NA, "4", "4")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
CurrentFrame <-
data.frame(Techniques_1 = c("1,3", "2,3", "2", "1", "2"),
Techniques_2 = c("2", "3", "2", "3", "2,3"),
Techniques_3 = c("4", "4", "3", "3", "2,3"),
Techniques_4 = c("2,3", "2,3", "3", "1", "1,2,3"),
Techniques_5 = c("1", "1", "1,2", "2", "1"),
Techniques_6 = c("1", "1", "1,2,3", "2", "1,2"),
stringsAsFactors = FALSE)
dput(CurrentFrame)
Console output from dput
structure(list(Techniques_1 = c("1,3", "2,3", "2", "1", "2"),
Techniques_2 = c("2", "3", "2", "3", "2,3"), Techniques_3 = c("4",
"4", "3", "3", "2,3"), Techniques_4 = c("2,3", "2,3", "3",
"1", "1,2,3"), Techniques_5 = c("1", "1", "1,2", "2", "1"
), Techniques_6 = c("1", "1", "1,2,3", "2", "1,2")), class =
"data.frame", row.names = c(NA,
-5L))