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

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

问题描述

我想用 read.table() 在 R 中导入一个表(.txt 文件).我的表格中的一列是一个包含九个数字的 ID - 一些 ID 以 0 开头,其他 ID 以 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天全站免登陆