如何在将变量传递给rownames()时保留变量的名称?

时间:2016-11-07 15:13:28

标签: r matrix dataframe

我确实跑了

my_matrix <- as.data.frame.matrix(xtabs(days_a_week ~ id_survey+service, 
data = my_table))

如果没有正确显示那些矩阵,如何修复该矩阵中的rownames?

这是我的数据集(音乐服务调查)的一部分,我制作了可重现的

service <- c("apple music", "spotify", "radio", "google play", "spotify", "spotify", "radio")
days_a_week <- c(1,2,1,1,3,2,1)
id_survey <- c("X38", "X38", "X38", "X22", "X22", "X50", "X50")

my_table <- as.data.frame(cbind(service,days_a_week))
setnames(my_table, colnames(my_table), c("service","days_a_week"))
my_table$id_survey <- id_survey
my_table$service <- as.character(my_table$service)
my_table$days_a_week <- as.numeric(as.character(my_table$days_a_week))

      service days_a_week id_survey
1 apple music           1       X38
2     spotify           2       X38
3       radio           1       X38
4 google play           1       X22
5     spotify           3       X22
6     spotify           2       X50
7       radio           1       X50

my_matrix <- as.data.frame.matrix(xtabs(days_a_week ~ id_survey+service, 
data = my_table))

   apple music google play radio spotify
X1           1           0     1       2
X2           0           1     0       3
X3           0           0     1       2

0 个答案:

没有答案