【发布时间】:2021-05-09 00:01:50
【问题描述】:
我有一个包含 376 列、7 行的 covid 数据框,其中包含 7 个国家/地区的 376 个不同天的 covid 感染数。我已经为它们匹配了不同的严重性类别,现在我正在尝试制作一个列联表,其中包含作为列的严重性类别和作为行的国家。我已经编写了一个函数并且它可以工作,但我仍然想知道是否有更优雅的解决方案可能包括一个 table() 函数,每行都有一个聚合。
我的代码:
severity <- function(x,countries){
sev = c("Leicht","Mittel","Schwer")
res=matrix(ncol=3,nrow=7)
colnames(res) = sev
rownames(res) = countries
for (i in 1:nrow(x)){
for (s in 1:length(sev)){
res[i,s]=length(x2[i,x2[i,]==sev[s]])
}
}
return(res)
}
r = severity(x2,covid_world2[,1]) #covid_world2 countains the countrynames, x2 the data with the categories
x = rbind(r,"Z" = colSums(r))
ctable=cbind(x,"S" = rowSums(x))
这只是 x2 中前两行的一个例子(即代表国家 Canada、Germany)
dput(head(covid_world2[, 1:20]))
输出是:
structure(list(Country = c("Canada", "France", "Germany", "Italy",
"Japan", "United Kingdom"), X1_22_20 = c(0, 0, 0, 0, 1.58132191886678e-08,
0), X1_23_20 = c(0, 0, 0, 0, 1.58132191886678e-08, 0), X1_24_20 = c(0,
3.06403006266968e-08, 0, 0, 1.58132191886678e-08, 0), X1_25_20 = c(0,
4.59604509400452e-08, 0, 0, 1.58132191886678e-08, 0), X1_26_20 = c(2.6495573093152e-08,
4.59604509400452e-08, 0, 0, 3.16264383773357e-08, 0), X1_27_20 = c(2.6495573093152e-08,
4.59604509400452e-08, 1.19354613321966e-08, 0, 3.16264383773357e-08,
0), X1_28_20 = c(5.2991146186304e-08, 6.12806012533936e-08, 4.77418453287863e-08,
0, 5.53462671603374e-08, 0), X1_29_20 = c(5.2991146186304e-08,
7.6600751566742e-08, 4.77418453287863e-08, 0, 5.53462671603374e-08,
0), X1_30_20 = c(5.2991146186304e-08, 7.6600751566742e-08, 4.77418453287863e-08,
0, 8.69727055376731e-08, 0), X1_31_20 = c(1.05982292372608e-07,
7.6600751566742e-08, 5.96773066609828e-08, 3.3078723093808e-08,
1.18599143915009e-07, 2.94611506927399e-08), X02_01_2020 = c(1.05982292372608e-07,
9.19209018800904e-08, 9.54836906575725e-08, 3.3078723093808e-08,
1.58132191886678e-07, 2.94611506927399e-08), X02_02_2020 = c(1.05982292372608e-07,
9.19209018800904e-08, 1.19354613321966e-07, 3.3078723093808e-08,
1.58132191886678e-07, 2.94611506927399e-08), X02_03_2020 = c(1.05982292372608e-07,
9.19209018800904e-08, 1.43225535986359e-07, 3.3078723093808e-08,
1.58132191886678e-07, 1.1784460277096e-07), X02_04_2020 = c(1.05982292372608e-07,
9.19209018800904e-08, 1.43225535986359e-07, 3.3078723093808e-08,
1.73945411075346e-07, 1.1784460277096e-07), X02_05_2020 = c(1.3247786546576e-07,
9.19209018800904e-08, 1.43225535986359e-07, 3.3078723093808e-08,
1.8185202066968e-07, 1.3257517811733e-07), X02_06_2020 = c(1.3247786546576e-07,
9.19209018800904e-08, 1.43225535986359e-07, 3.3078723093808e-08,
1.8185202066968e-07, 1.3257517811733e-07), X02_07_2020 = c(1.85469011652064e-07,
9.19209018800904e-08, 1.55160997318555e-07, 4.9618084640712e-08,
1.8185202066968e-07, 1.3257517811733e-07), X02_08_2020 = c(1.85469011652064e-07,
1.68521653446832e-07, 1.55160997318555e-07, 4.9618084640712e-08,
1.89758630264014e-07, 1.9149747950281e-07), X02_09_2020 = c(1.85469011652064e-07,
1.68521653446832e-07, 1.67096458650752e-07, 4.9618084640712e-08,
1.89758630264014e-07, 2.06228054849179e-07)), row.names = c(NA,
6L), class = "data.frame")
【问题讨论】:
-
只是想指出,在 x2 中,值的范围被归类为“Leicht”、“Mittel”或“Schwer”。
-
此输出已更改,因此我无法访问数据。我了解您可能担心列数。所以你可以用
dput(head(covid_world2[, 1:20]))来获取前20 列。这应该足以创建可重现的示例。 -
顺便说一句,我使用此功能进行分类:
severeness <- function(v){ for (i in 1:length(v)){ if(v[i]< 0.01){ v[i]="Leicht" } else if(v[i]>= 0.01 & v[i]<0.05 ){ v[i]="Mittel" } else if(v[i]>0.05){ v[i]="Schwer" } } return(v) } x2=apply(covid_world2[,-1],2,severeness)
标签: r dataframe aggregation contingency