防止R将文本解释为数字 [英] Preventing R from interpreting text as numeric

查看:98
本文介绍了防止R将文本解释为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在R中导入包含邮政编码信息的CSV,当我需要它作为字符时,它会解释为数字。

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)

数据格式如下:

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

现在发生了什么,R将前2列解释为数字,并将其转换为以下形式:

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


推荐答案

使用 colClasses 参数 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

这篇关于防止R将文本解释为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆