1

我在尝试用两组创建李克特图时遇到问题。我在两个社区实现了一项调查。我现在想比较这两个社区。[那是我的数据框][1]。到目前为止,我已经加载了一张包含 3 列的表格。一列指的是位置(community1,community2),一列包括关于 econ_comm (1-6) 的答案,一列包括关于 future_persp (1-6) 的答案。我创建了一个李克特对象和第一个图。

g_likert = likert(g[1:6])
plot(g_likert, ordered = FALSE, group.order = names(g[2:3]))

......它奏效了。以下是我到目前为止得到的。[![在此处输入图像描述][2]][2]

我认为现在首先创建一个双对象很重要:(both<-g$Location有效)

现在我开始遇到麻烦了。以下代码向我显示了错误:

both_likert_2 = likert(both[, c(1:3), drop=FALSE], grouping = both$location)
plot(both_likert_2, include.histogram = TRUE)

错误是:

[.data.frame (g, 1:6) 中的错误:选择了未定义的列

[.default (both, , c(1:3), drop = FALSE) 中的错误:维数错误 - 未找到 Objekt 'both_likert_2'

[我现在还附上了我的 R 的屏幕截图,以确保。][4] 我现在挣扎了很长一段时间,我将非常感谢您的帮助。最好的,菲利克斯

编辑: [![这是我在 R 中的当前情况][5]][5] 这是我重现它的代码:

library(likert)
g<-read.csv2("C:/Users/felix/OneDrive/Documents/R/SurveyData2.csv", sep=";", dec=",", header=TRUE)
both<-g$Location
g <-  within(g, {
  gold_21cent <- factor(gold_21cent, levels=1:6, labels=c("Completely agree", "Agree", "Slightly agree", "Slightly disagree", "Disagree", "Completely disagree"))
  future_persp <- factor(future_persp, levels=1:6, labels=c("Completely agree", "Agree", "Slightly agree", "Slightly disagree", "Disagree", "Completely disagree"))
 jobs_comm <- factor(jobs_comm, levels=1:6, labels=c("Completely agree", "Agree", "Slightly agree", "Slightly disagree", "Disagree", "Completely disagree"))
} )
.........etc............
comm_likert = likert(g[,2:14], grouping=g[,1])
plot(comm_likert)
library(dplyr)
g %>%

重命名(It offers important economic perspectives=future_persp, It provides economic prosperity to the community=econ_comm)%>%likert(分组=位置)%>%情节()

编辑:使用 dput(g)

structure(list(Location = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("Huan", "TP"), class = "factor"), 
gold_21cent = structure(c(6L, 6L, 3L, 3L, 2L, 2L, 2L, 2L, 
2L, 6L, 3L, 2L, 2L, 3L, 2L, 3L, 1L, 3L, 2L, NA, NA, 1L, 1L,
6L, 4L, 6L, 5L, 2L, 2L, 2L, 4L, 4L, 3L, 3L, 2L, 3L, 2L, 3L, 
3L, NA, 2L, 3L, 2L, NA, 2L, 2L, 3L, 5L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 2L, 3L, 6L), .Label = c("Completely agree", "Agree", 
"Slightly agree", "Slightly disagree", "Disagree", "Completely disagree"
), class = "factor"), life_quality = structure(c(3L, 3L, 
6L, 6L, 5L, 5L, 6L, 5L, 6L, 3L, 3L, 6L, 4L, 6L, 6L, 6L, 6L, 
6L, 6L, NA, NA, 3L, 5L, 6L, 2L, 6L, 5L, 3L, 2L, 3L, 1L, 1L, 
2L, 2L, 3L, 1L, 3L, 2L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 2L, NA, 
3L, 2L, 2L, 3L, 3L, 3L, 5L, 5L, 3L, 2L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), coexist_tradact = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 2L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 5L, 5L, 6L, 2L, 6L, 1L, 6L, 5L, 6L, 6L, 6L, 1L, 
4L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 4L, 3L, 2L, 3L, 3L, 3L, 
4L, 4L, 3L, 2L, 4L, 5L, 4L, 2L, 4L, 6L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), emigration_comm = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 5L, NA, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, NA, NA, 4L, 2L, 1L, 3L, 1L, 1L, 6L, 6L, 6L, 2L, 
1L, 3L, 3L, 3L, 1L, 3L, 1L, 2L, 1L, 2L, 2L, 3L, 3L, 2L, 1L, 
4L, 5L, 5L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), future_persp = structure(c(3L, 
3L, 5L, 5L, 5L, 5L, 6L, 5L, 5L, 3L, 5L, 6L, 6L, 5L, 3L, 6L, 
5L, 5L, 3L, 3L, 3L, 2L, 1L, 6L, 3L, 6L, 5L, 3L, 3L, 3L, 1L, 
3L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 2L, 4L, 2L, 2L, 3L, 1L, 2L, 
4L, 4L, 4L, 2L, 4L, 2L, 3L, 5L, 5L, 3L, 2L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), workers_comm = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 2L, 6L, 6L, 
6L, 6L, 6L, 4L, 5L, NA, 1L, 6L, 1L, 6L, 5L, 6L, 6L, 6L, 1L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 1L, 4L, 3L, 4L, 5L, 3L, 3L, 4L, 4L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), work_project = structure(c(6L, 
6L, 6L, 6L, 5L, 5L, 6L, 5L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 5L, 3L, 6L, 4L, 6L, 5L, 6L, 6L, 6L, 6L, 
3L, 2L, 2L, 1L, 1L, 3L, 6L, 3L, 3L, 2L, 6L, 3L, 3L, 3L, 1L, 
3L, 6L, 5L, 6L, 5L, 1L, 6L, 6L, 5L, 6L, 2L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), agree_comm = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 6L, 5L, 6L, 
6L, 6L, 6L, 5L, 5L, 5L, 3L, 6L, 2L, 5L, 6L, 6L, 6L, 6L, 3L, 
3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 4L, 1L, 3L, NA, 3L, 2L, 
3L, 3L, NA, 3L, 3L, 4L, 3L, 3L, 3L, 4L, 2L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), informed_deals = structure(c(4L, 
4L, 1L, 1L, 5L, 6L, 5L, 5L, 5L, 6L, 4L, 5L, 5L, 6L, 4L, 6L, 
5L, 6L, 4L, 6L, 6L, 6L, 1L, 6L, 3L, 6L, 1L, 2L, 2L, 2L, 4L, 
2L, 3L, 3L, 2L, 1L, 5L, 6L, 5L, 2L, 3L, 5L, 3L, 3L, 4L, 4L, 
2L, 4L, 4L, 5L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), fear_environ = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 5L, 1L, 
1L, 1L, 1L, 6L, 6L, 1L, 2L, 1L, 4L, 1L, NA, 1L, 1L, 1L, 4L, 
4L, 3L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 1L, 
NA, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 2L, 2L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), water_quant = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 1L, 2L, 1L, 
1L, 1L, 6L, 6L, 6L, 5L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
3L, 2L, 1L, 2L, 3L, 1L, 3L, 2L, 3L, 1L, 3L, 3L, NA, 2L, 1L, 
5L, 1L, 1L, 2L, 4L, 1L, 3L, 1L, 2L, 3L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), support_govern = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 5L, 6L, 6L, 4L, 5L, 6L, 1L, 5L, 6L, 3L, 3L, 3L, 3L, 
3L, 2L, 3L, 3L, 2L, 5L, 5L, 3L, 2L, 4L, 3L, 3L, NA, 1L, 1L, 
2L, 6L, 3L, 3L, 3L, 4L, 4L, 6L, 4L, 4L, 3L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), proud_comm = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 1L, 1L, 1L, 
3L, 1L, 2L, 1L, 1L, 2L, 5L, 2L, 1L, 2L, 2L, 2L, NA, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 3L, 1L, 2L, 1L, 3L, 2L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), econ_comm = structure(c(6L, 
6L, 6L, 6L, 5L, 6L, 5L, 5L, 6L, 6L, 3L, 6L, 6L, 5L, 3L, 5L, 
3L, 4L, 5L, 2L, 2L, 5L, 5L, 6L, 2L, 6L, 5L, 5L, 5L, 5L, 3L, 
2L, 3L, 3L, 1L, 1L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 1L, 
3L, 2L, 3L, 1L, 5L, 3L, 2L, 5L, 2L, 6L, 3L, 4L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), jobs_comm = structure(c(6L, 
6L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 4L, 6L, 4L, 
6L, 4L, 5L, 5L, 5L, 3L, 2L, 6L, 1L, 6L, 5L, 5L, 5L, 5L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 
2L, 2L, 2L, 1L, 5L, 2L, 3L, 4L, 2L, 2L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), inequality_comm = structure(c(1L, 
1L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 1L, 3L, 6L, 1L, 2L, 6L, 2L, 
6L, 2L, 6L, NA, NA, 5L, 1L, 6L, 3L, 6L, 5L, 5L, 5L, 5L, 1L, 
3L, 3L, 2L, 3L, 4L, 2L, 3L, 4L, 3L, 2L, 4L, 3L, 3L, 5L, 3L, 
4L, 5L, 3L, 1L, 4L, 3L, 4L, 2L, 3L, NA, 3L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), exp_growth = structure(c(6L, 
6L, 4L, 4L, 6L, 6L, 6L, 5L, 6L, 6L, 4L, 4L, 6L, 2L, 2L, 2L, 
5L, 2L, 3L, NA, NA, 3L, 3L, 6L, 1L, 6L, 5L, 3L, 2L, 6L, 4L, 
2L, 2L, 3L, 2L, 3L, 5L, 2L, 3L, 2L, 3L, 4L, 2L, 3L, 2L, 1L, 
2L, 5L, 3L, 4L, 2L, 4L, 2L, 6L, 3L, 3L, 3L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), future_region = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
5L, 6L, 5L, 2L, 2L, 2L, 2L, 6L, 3L, 6L, 5L, 6L, 6L, 3L, 4L, 
2L, 3L, 3L, 2L, 4L, 5L, 5L, 5L, 2L, 4L, 2L, 4L, 3L, 4L, 1L, 
NA, 6L, 3L, 3L, 3L, 4L, 3L, 4L, 5L, 5L, 2L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), contamination_environ = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 1L, 6L, 1L, 
6L, 1L, 1L, 6L, 6L, 6L, 4L, 1L, 4L, 6L, 1L, 1L, 1L, 1L, 4L, 
1L, 2L, 1L, 3L, 3L, 1L, 2L, 1L, 3L, 3L, 5L, 2L, 2L, 1L, 1L, 
NA, 1L, 1L, 1L, 1L, 5L, 2L, 1L, 3L, 2L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), conflicts_comm = structure(c(6L, 
6L, 1L, 1L, 6L, 6L, 6L, 6L, 6L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 
2L, 1L, 6L, 5L, 5L, 5L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 4L, 
3L, 3L, 3L, 3L, 1L, 3L, 2L, 4L, 3L, 3L, 4L, 3L, NA, 4L, 2L, 
3L, 2L, 3L, 3L, 5L, 4L, 4L, 1L, 3L, 5L, 3L, 4L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), infrastructure_comm = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 4L, 6L, 5L, 6L, 5L, 6L, 
5L, 6L, 3L, 3L, 3L, NA, 1L, 6L, 1L, 6L, 6L, 4L, 4L, 3L, 1L, 
4L, 3L, NA, 3L, 1L, 5L, 6L, 2L, 3L, 3L, 4L, 3L, 2L, 1L, 2L, 
2L, 4L, 2L, 3L, 6L, 4L, 3L, 3L, 3L, 3L, 2L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), included_mining = structure(c(1L, 
1L, 1L, 1L, 5L, 6L, 6L, 6L, 6L, 6L, 3L, 6L, 6L, 6L, 5L, 6L, 
6L, 6L, 3L, 5L, 5L, 6L, 3L, NA, 3L, NA, NA, 6L, 5L, 5L, 1L, 
3L, 3L, 3L, 3L, 5L, 4L, 6L, 3L, 3L, 2L, 6L, 2L, NA, 2L, 3L, 
2L, 5L, 5L, 3L, 5L, 3L, 3L, 2L, 4L, 5L, 3L, NA), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), investment_region = structure(c(1L, 
1L, 3L, 3L, 6L, 6L, 6L, 5L, 6L, 1L, 2L, 1L, 6L, 2L, 4L, 1L, 
2L, 2L, 3L, NA, NA, 4L, 1L, 5L, 3L, 5L, 5L, 1L, 2L, 2L, 4L, 
2L, 2L, 2L, 4L, 1L, 2L, 3L, 3L, 2L, 3L, 1L, 3L, NA, 3L, 2L, 
NA, 2L, 3L, 3L, 4L, 4L, 3L, 3L, 2L, 2L, 2L, 4L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), water_qual = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 6L, 5L, 5L, 5L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
4L, 3L, 1L, 2L, 3L, 1L, 3L, 2L, 3L, 1L, 3L, 3L, NA, 3L, 1L, 
4L, 1L, 1L, 3L, 4L, 2L, 2L, 1L, 2L, 3L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor")), row.names = c(NA, 

-58L), 类 = "data.frame")

4

1 回答 1

0

我想你只是犯了一个简单的打字错误。而且您实际上不需要创建both向量,因为您可以在函数调用中传递分组变量。尝试:

both_likert_2 = likert(g[, c(1:3), drop=FALSE], grouping = g$Location)

编辑:根据提供的新信息,这个怎么样:

g <-  within(g, {
  future_persp <- factor(future_persp, levels=1:6)
  econ_comm <- factor(econ_comm, levels=1:6)
} )

comm_likert = likert(g[,2:3], grouping=g[,1])
plot(comm_likert)

编辑:OP想要将y轴刻度标签更改为更合适的东西。更改列的名称,但使用 dplyr 以便您不必进行永久更改:

library(dplyr)
g %>%
   dplyr::select(future_persp, econ_comm) %>%
   rename(`It offers important economic perspectives`=future_persp,
          `It provides economic prosperity to the community`=econ_comm) %>%
   likert(grouping=g[, "Location"]) %>%
   plot()
于 2020-02-10T08:35:53.790 回答