Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 关于使用as.double()函数将因子转换为数值变量的思考_R_Ggplot2_Dplyr - Fatal编程技术网

R 关于使用as.double()函数将因子转换为数值变量的思考

R 关于使用as.double()函数将因子转换为数值变量的思考,r,ggplot2,dplyr,R,Ggplot2,Dplyr,我想比较同一所学校的性别表现。然而,R不断地将我的“男性大学分数”列作为一个因素导入,因此为了解决这个问题,我编写了以下代码: 该代码在过去曾为我解决过类似的问题,但在本例中,as.double()无法有效地工作,因为它将Mel_分数转换为从最高到最低的排名(即1,2,3,4等) 我进一步尝试了as.numeric(),但不幸的是,它也不起作用 library(ggplot2) library(readxl) library(tidyr) library(dplyr) library("ggal

我想比较同一所学校的性别表现。然而,R不断地将我的“男性大学分数”列作为一个因素导入,因此为了解决这个问题,我编写了以下代码:

该代码在过去曾为我解决过类似的问题,但在本例中,as.double()无法有效地工作,因为它将Mel_分数转换为从最高到最低的排名(即1,2,3,4等)

我进一步尝试了as.numeric(),但不幸的是,它也不起作用

library(ggplot2)
library(readxl)
library(tidyr)
library(dplyr)
library("ggalt")

gender_comparison <- read.csv(file = "dumbbell_plot.csv")

# Change variables from character into numeric format  
gender_comparison <-  gender_comparison %>% 
  mutate(male_scores = as.double(male_scores))

# Check format of variables
sapply(gender_comparison, class)

## Create a dumbbell plot to compare boys and girls performance within the same school
ggplot(gender_comparison, aes(x=male_scores, xend=female_scores, y=school_name,)) + 
  geom_dumbbell(size=1,color="grey", 
                colour_x = "blue", colour_xend = "red",
                dot_guide=TRUE, dot_guide_size=0.20) +
  scale_x_continuous(limits = c(70,90)) +
  labs(x="Average Exam Scores", y="City", 
       title="The Gender Standardized Exams Score Gap Remains Prevalent Within Schools", 
  theme(panel.grid.major.x=element_line(size=0.20)) +
  theme(panel.grid.major.y=element_blank())+
  theme(axis.text.y=element_text(size = rel(0.55)))
库(ggplot2)
图书馆(readxl)
图书馆(tidyr)
图书馆(dplyr)
图书馆(“ggalt”)

性别比较一种解决方案可能只是将
stringsAsFactors=FALSE
传递给
read.csv
。此外,您还可以查看
帮助(as.numeric)
以获取将因子标签转换为数值的建议:
as.numeric(levels(male_分数))[male_分数]
有关将因子转换为数值的详细信息,请参阅