在R中具有数字数据到分类数据

我有调查数据,其中涉及参与者的回答。

我的数据格式为4 3 2 3 3 3 3 3 3 3 3 4 3 3 3 3 3 3 2 3 3

或者,以dput格式:

c(4,3,2,4,3)

但是我想将它们归类为

strongly disagree = 1 
disagree = 2 
agree = 3 
strongly agree = 4 

,以便在进行直方图绘制时可以使用不同的颜色。

我尝试使用此方法

datatat = survey$Q1A 
catsurvey <- cut(datatat,breaks =c(0,4),labels=c("Disagree","Agree","Strongly Agree"))    
hist(datatat,main="Distribution of Player Ratings",xlab="Responses",border = "black",col = c("blue","red","green"))  
zyuncx250 回答:在R中具有数字数据到分类数据

尝试以下方法。由于您不想创建数据框,因此我为您发布的数据取了另一个名字。

x <- scan(text = '4 3 2 3 3 3 3 3 3 3 4 3 3 3 3 3 3 2 3 3')
labs <- c('strongly disagree','disagree','agree','strongly agree')

datatat <- factor(x,levels = 1:4,labels = labs)
tbl <- table(datatat)

barplot(tbl[tbl != 0],main = "Distribution of Player Ratings",xlab = "Responses",border = "black",col = c("blue","red","green"))

enter image description here

本文链接:https://www.f2er.com/3163324.html

大家都在问