李克特数据的R频率表



我有一个我认为是基本任务的任务,但事实证明并非如此。 我有一系列调查,我需要将其转换为每个调查的频率表。 例如,调查 1 由 6 个问题组成,参与者有 5 个回答选项。 对于每个调查,我需要生成一个包含每个问题的表格(在本例中为 6 个),以及每个问题回答每个回答选项的参与者百分比。

我一直在使用 prop.table,但一次只能对一个问题执行此操作,并且我还没有弄清楚如何添加百分号,并且我在行名称中丢失了问题变量标题。

总的来说,我想将这些表格直接打印到一个word文档中。 这部分我想我已经想通了,但现在我需要弄清楚表格。

我欢迎任何建议。谢谢!

编辑

以下是我到目前为止使用的一些李克特样本数据:

q1<-c(2,2,3,3,3,4,4,4,5,5)
q2<-c(2,3,3,4,4,4,4,5,5,5)
q3<-c(2,2,2,3,4,4,4,5,5,5)
df<-data.frame(q1,q2,q3)
x<-prop.table(table(factor(df$q1,levels=1:5)))*100
y<-round(x,digits=1)`

这会产生类似于我需要的东西。但是,我希望"q1"作为行名出现在结果表中,我希望百分比有一个 % 符号,并且我需要一种方法将另外两个"q2"q3"行合并到同一个表中。

希望有帮助。谢谢。

q1<-c(2,2,3,3,3,4,4,4,5,5)
q2<-c(2,3,3,4,4,4,4,5,5,5)
q3<-c(2,2,2,3,4,4,4,5,5,5)
df<-data.frame(q1,q2,q3)
library(expss)
# add value lables for preserving empty categories
val_lab(df) = autonum(1:5)
res = df
for(each in colnames(df)){
res = res %>% 
tab_cells(list(each)) %>% 
tab_cols(vars(each)) %>% 
tab_stat_rpct(total_row_position = "none")
}

res = res %>% tab_pivot() 
# add percentage sign
recode(res[,-1]) = other ~ function(x) ifelse(is.na(x), NA, paste0(round(x, 0), "%"))
res
# |    |  1 |   2 |   3 |   4 |   5 |
# | -- | -- | --- | --- | --- | --- |
# | q1 |    | 20% | 30% | 30% | 20% |
# | q2 |    | 10% | 20% | 40% | 30% |
# | q3 |    | 30% | 10% | 30% | 30% |

如果您使用knitr则以下代码将很有帮助:

library(knitr)
res %>% kable

我不建议你这样做,因为它对以后的争吵没有用,但为了完全按照要求......

for (i in seq_along(names(df))) {
assign(paste0("x",i), prop.table(table(factor(df[[i]], levels = 1:5))))
}
result <- rbind(x1, x2, x3)
rownames(result) <- names(df)
as.data.frame(matrix(
sprintf("%.0f%%", result*100), 
nrow(result), 
dimnames = dimnames(result)
))
1   2   3   4   5
q1 0% 20% 30% 30% 20%
q2 0% 10% 20% 40% 30%
q3 0% 30% 10% 30% 30%

最后一段代码如此处建议。

在不知道数据是什么样子的情况下,很难给出准确的答案。但是,假设我已经有某种数据框,我将从创建将数据系统地转换为绘图的函数开始。我也会使用 ggplot2 而不是基本的 R 图形,因为它会更灵活。


假设您有每项调查的数据框。根据我的经验,您可能会有一行,其中一列指示一个问题,另一列表示对该问题的给定响应。

那是:

survey = data.frame(question = factor(rep(1:6,4)),response = factor(c(1:5,sample(1:5,19, replace = TRUE))))

然后,您可以创建一个函数,该函数在给定上述数据框的情况下计算问题中每个响应的百分比

library(plyr)
# Assumes survey has columns question and response
calculate_percent = function(survey){
ddply(survey, ~question, function(rows){ 
total_responses = nrow(rows)
response_percent =  ddply(rows, ~response, function(rows_response){
count_response = nrow(rows_response)
data.frame(response = unique(rows_response$response), percent = (count_response/total_responses)*100)
})
data.frame(question = unique(rows$question), response_percent)
})
}

然后,您可以创建一个函数,该函数在给定数据框的情况下制作绘图,如上面定义的数据框。

library(ggplot2)
library(scales)
percentage_plot = function(survey){
calculated_percentages = calculate_percent(survey)
ggplot(calculated_percentages,aes(x = question, y = percent)) + 
geom_bar(aes(fill = response),stat = "identity",position = "dodge") +
scale_y_continuous(labels = percent)
}

最终可以与调用一起使用

percentage_plot(survey)

然后,由于您有多个调查,因此您可以使用其他函数进行概括,这些功能将以与上述类似的方式系统地处理数据。

您也可以在分面中完成上述图,而不是在此处的分组箱形图。但是,由于您有多个调查,因此您可能希望在该级别使用分面。


引用:

ggplot 百分比

ggplot 分组条形图


抱歉,我在编辑之前就开始编写示例,希望您仍然可以根据您的用例进行自定义。

实际上,我似乎误解了您的问题并回答了另一个问题。

最新更新