Preventing R from interpreting text as numeric

自闭症网瘾萝莉.ら 提交于 2020-02-16 06:45:09

问题


I am trying to import a CSV in R which has ZIP code information which R is interpreting as numeric when I need it to remain as character.

data = read.csv("zipCodeInformation.csv", stringsAsFactor = FALSE)

The data has the following format:

Lower.Zip, Upper.Zip, Zone
004,       005,       Zone.8
006,       007,       Zone.45
009,          ,       Zone.45
010,       089,       Zone.8
100,       339,       Zone.8

What happens right now is R interprets the first 2 columns as numeric and turns them into the following:

Lower.Zip, Upper.Zip, Zone
4,         5,         Zone.8
6,         7,         Zone.45
9,          ,         Zone.45
10,        89,        Zone.8
100,       339,       Zone.8

回答1:


Use the colClasses argument to read.csv.

Data <- read.csv(text="Lower.Zip, Upper.Zip, Zone
004,       005,       Zone.8
006,       007,       Zone.45
009,          ,       Zone.45
010,       089,       Zone.8
100,       339,       Zone.8",
colClasses=rep("character",3))  # you may want to add strip.white=TRUE


来源:https://stackoverflow.com/questions/21616719/preventing-r-from-interpreting-text-as-numeric

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!