首页 > 解决方案 > 如何将 id 列表作为列表而不将它们更改为整数

问题描述

我有一个我正在使用的调度代码列表,并且正在尝试绘制一个条形图。

问题是它们具有类似的值29D00V,当我使用类型检查它们的类型时,它们会以整数形式出现。

我想将它们绘制为条形图的 x 轴上的分类,但是当我将它们转换为列表时,c(rows_to_use$Group.1)它会将它们转换为整数。

例如,我怀疑它正在将它们读取为十六进制24D01568 ,然后将它们转换为整数。有没有一种简单的方法可以将数据框的整个 Group.1 列更改为字符串类型?

这是我的脚本的当前迭代:

#read csv
df <-        read.csv(file="C:/Users/GIGNWI_Ar208671/Documents/R/Projects/CallTimes/call_times/data/risks_codes_descriptions.csv",header=TRUE)
#correct garbled column name
colnames(df)[1] = "call_times"
rows_to_use <-df
#initialise new name, this is where filter will be applied if applicable
rows_to_use = aggregate(x = rows_to_use$call_times,by=    (list(rows_to_use$DispatchCodeAndSuffix)),median)
#Take the median of each dispatch code in the list
rows_to_use <- rows_to_use[order(-rows_to_use$x),]
#Order by largest to smallest median call time
rows_to_use <-head(rows_to_use,100)
#Take the top 100
x = c(rows_to_use$x)
y = c(rows_to_use$Group.1)
#Create vectors for bar plot
barchart(x, names.arg=y)
#Plot barchart
print(2)

标签: r

解决方案


没关系,如果其他人有这个问题,我就解决了,只需使用 colClasses 指定 read.csv 中列的数据类型,例如

 df <-read.csv(file="C:/Users/GIGNWI_Ar208671/Documents/R/Projects/CallTimes/call_times/data/risks_codes_descriptions.csv",header=TRUE,colClasses=c("DispatchCodeAndSuffix"="character"))

推荐阅读