如何在R中从频率表转换为原始数据
假设我有一个频率表:如何在R中从频率表转换为原始数据,r,frequency,R,Frequency,假设我有一个频率表: t = matrix(c(20,10,5,15), ncol=2, dimnames=list(c("yes","no"), c("yes","no"))) t yes no yes 20 5 no 10 15 我想把频率表转换回原始数据。我的代码是(不起作用): 有人能告诉我怎么了吗 我想您应该使用meltfromrestrape2: test = matrix(c(20,10,5,15), ncol=2, dimnames=list(c("yes","
t = matrix(c(20,10,5,15), ncol=2, dimnames=list(c("yes","no"), c("yes","no")))
t
yes no
yes 20 5
no 10 15
我想把频率表转换回原始数据。我的代码是(不起作用):
有人能告诉我怎么了吗 我想您应该使用
melt
fromrestrape2
:
test = matrix(c(20,10,5,15), ncol=2, dimnames=list(c("yes","no"), c("yes","no")))
test
# yes no
#yes 20 5
#no 10 15
library(reshape2)
melt(test)
# Var1 Var2 value
#1 yes yes 20
#2 no yes 10
#3 yes no 5
#4 no no 15
mat如果我们想要20行表示“是”的“是”,10行表示“否”的“是”…@用户查看调用矩阵t
不是一个好主意,因为t
是一个主要用于矩阵的函数。像t(t)%*%t
这样的东西不太可读。
test = matrix(c(20,10,5,15), ncol=2, dimnames=list(c("yes","no"), c("yes","no")))
test
# yes no
#yes 20 5
#no 10 15
library(reshape2)
melt(test)
# Var1 Var2 value
#1 yes yes 20
#2 no yes 10
#3 yes no 5
#4 no no 15
mat <- matrix(c(20,10,5,15), ncol=2, dimnames=list(c("yes","no"), c("yes","no")))
a <- rep(rep(c("yes", "no"), 2), c(mat))
b <- rep(c("yes", "no"), colSums(mat))
a <- factor(a, levels=c("yes", "no"))
b <- factor(b, levels=c("yes", "no"))
table(a, b)
b
a yes no
yes 20 5
no 10 15