如何使用R将分类数据转换为相对计数
最小示例如何使用R将分类数据转换为相对计数,r,R,最小示例 我有:input这不是一个完整的答案,但应该会对您有所帮助(有点resphape[2]-ing: ct <- count(input, "id") A <- data.frame(table(input[,c(1,2)])/ct[ct$id==1,]$freq) B <- data.frame(table(input[,c(1,3)])/ct[ct$id==2,]$freq) print(A) id A Freq 1 1 1 0.6666667 2
我有:
input这不是一个完整的答案,但应该会对您有所帮助(有点resphape[2]
-ing:
ct <- count(input, "id")
A <- data.frame(table(input[,c(1,2)])/ct[ct$id==1,]$freq)
B <- data.frame(table(input[,c(1,3)])/ct[ct$id==2,]$freq)
print(A)
id A Freq
1 1 1 0.6666667
2 2 1 0.3333333
3 1 2 0.3333333
4 2 2 0.3333333
5 1 3 0.0000000
6 2 3 0.3333333
print(B)
id B Freq
1 1 0 0.3333333
2 2 0 0.6666667
3 1 1 0.6666667
4 2 1 0.3333333
ct此表达式为每个非ID列创建一个表(此处,2:3
):
id
列不存在,但行名称可用于此目的。以下是可能的解决方案:
library(reshape2)
library(qdap)
x <- prop.table(ftable(melt(input, id="id")))
x2 <- colpaste2df(data.frame(x), 2:3, keep.orig = FALSE, sep="", name.sep = "")
x3 <- dcast(x2, id ~ variablevalue, value.var = "Freq")
x3[, c(TRUE, colSums(x3[, -1]) != 0)]
## id A1 A2 A3 B0 B1
## 1 1 0.16666667 0.08333333 0.00000000 0.08333333 0.16666667
## 2 2 0.08333333 0.08333333 0.08333333 0.16666667 0.08333333
library(重塑2)
图书馆(qdap)
x可以看作是一个数据透视表(或两个数据透视表):
>install.packages('重塑')
>图书馆(重塑)
>ct DF1DF2DF3名称(DF3)DF3
id A1 A2 A3 B0 B1
1 1 0.6666667 0.3333333 0.0000000 0.3333333 0.6666667
2 2 0.3333333 0.3333333 0.3333333 0.6666667 0.3333333
我想这正是您想要的。只需添加行或列名即可满足您的口味
tbB <- with(input, table(B, id))
tbA <- with(input, table(A, id))
cbind( t( tbA/rowSums(tbA)), t(tbB/rowSums(tbB)) )
1 2 3 0 1
1 0.6666667 0.5 0 0.3333333 0.6666667
2 0.3333333 0.5 1 0.6666667 0.3333333
tbB非常直截了当+1谢谢Matthew Lundberg.table(dat)/sum(dat)
是通常的方法。或者您可能想除以行和数
或列和数
,但所需的输出并不能阐明您的意图。为什么第二列不是0.5、0.5?@Ishouldbuyabat:列A2表示用户为变量a选择选项2的次数比例。Id=1和Id=2每次都选择选项2ee他们做出的选择。谢谢!谢谢!当我尝试library(qdap)时,我得到一个错误,说R和Java没有匹配的体系结构。我可能可以解决这个问题,但现在我将尝试其他解决方案。请参阅以下帮助:
do.call(cbind, individuals)
## A1 A2 A3 B0 B1
## 1 0.6666667 0.3333333 0.0000000 0.3333333 0.6666667
## 2 0.3333333 0.3333333 0.3333333 0.6666667 0.3333333
library(reshape2)
library(qdap)
x <- prop.table(ftable(melt(input, id="id")))
x2 <- colpaste2df(data.frame(x), 2:3, keep.orig = FALSE, sep="", name.sep = "")
x3 <- dcast(x2, id ~ variablevalue, value.var = "Freq")
x3[, c(TRUE, colSums(x3[, -1]) != 0)]
## id A1 A2 A3 B0 B1
## 1 1 0.16666667 0.08333333 0.00000000 0.08333333 0.16666667
## 2 2 0.08333333 0.08333333 0.08333333 0.16666667 0.08333333
>install.packages('reshape')
>library(reshape)
>ct <-count(input, "id")
>DF1<-cast(input, id ~ A, value='B')
>DF2<-cast(input, id ~ B, value="A")
>DF3<-cbind(DF1$id, DF1[names(DF1)!='id']/ct[1,]$freq, DF2[names(DF2)!='id']/ct[2,]$freq)
>names(DF3)<-c('id', paste('A', names(DF1)[-1], sep=''), paste('B', names(DF2)[-1], sep=''))
> DF3
id A1 A2 A3 B0 B1
1 1 0.6666667 0.3333333 0.0000000 0.3333333 0.6666667
2 2 0.3333333 0.3333333 0.3333333 0.6666667 0.3333333
tbB <- with(input, table(B, id))
tbA <- with(input, table(A, id))
cbind( t( tbA/rowSums(tbA)), t(tbB/rowSums(tbB)) )
1 2 3 0 1
1 0.6666667 0.5 0 0.3333333 0.6666667
2 0.3333333 0.5 1 0.6666667 0.3333333