【问题标题】:How to put list of id's as a list without them being changed into integers如何将 id 列表作为列表而不将它们更改为整数
【发布时间】:2018-07-30 23:39:27
【问题描述】:

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

问题是它们的值类似于 29D00V,当我使用 type of 检查它们的类型时,它们会显示为整数。

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

例如24D01 变为 568 我怀疑它正在将它们读取为十六进制值,然后将它们转换为整数。有没有一种简单的方法可以将数据框的整个 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)

【问题讨论】:

  • 不要编辑您的问题以将答案包含在正文中,您应该将其发布在下面作为答案。在 24 小时内(对于自答问题),您可以“接受”将问题标记为已完成的答案。
  • 谢谢 Gregor,我现在已经改了。相当新,所以仍然要掌握规则

标签: r


【解决方案1】:

没关系,如果其他人有这个问题,我解决了它,只需使用 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"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2021-06-06
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    相关资源
    最近更新 更多