如何避免:read.table会截断以0开头的数值 [英] How to avoid: read.table truncates numeric values beginning with 0

查看:157
本文介绍了如何避免:read.table会截断以0开头的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用read.table()导入R中的表(.txt文件).我表中的一列是带有9个数字的ID-一些ID以0开头,另一些以1或2开头.

I want to import a table (.txt file) in R with read.table(). One column in my table is an ID with nine numerals - some ids begin with a 0, other with 1 or 2.

R将第一个0截断(012345678变为12345678),这会导致在使用此ID合并另一个表时出现问题.

R truncates the first 0 (012345678 becomes 12345678) which leads to problems when using this ID to merge another table.

有人可以给我提示如何解决这个问题吗?

Can someone give me a hint how to solve the problem?

推荐答案

如Ben的回答所述,colClasses是更简单的方法.这是一个示例:

As said in Ben's answer, colClasses is the easier way to do it. Here is an example:

read.table(text = 'col1 col2
           0012 0001245',
           head=T,
           colClasses=c('character','numeric'))

  col1 col2
1 0012 1245      ## col1 keep 00 but not col2

这篇关于如何避免:read.table会截断以0开头的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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