Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R read file which contains column with 001000 values

Tags:

file

dataframe

r

I'd like to read a file which contain 2 columns.

2 00001
9 00001
3 00001
12 00001
115 00001
11 00001
12 00001
38 00001

if I use the standard read.table, I end up with something like that :

   V1 V2
1   2  1
2   9  1
3   3  1
4  12  1
5 115  1
6  11  1

Do you have any idea on how I could read this file, and keep the 2nd column as it is? Thanks

like image 713
Benoit B. Avatar asked Feb 26 '23 04:02

Benoit B.


2 Answers

Read the documentation for read.table() and learn about how select column types. You want the second column as character.

like image 191
Dirk Eddelbuettel Avatar answered Mar 11 '23 16:03

Dirk Eddelbuettel


Looks like you can pass an argument as.is to change whether read.table attempts to parse strings into values, or keep them as raw strings.

as.is the default behavior of read.table is to convert character variables (which are not converted to logical, numeric or complex) to factors. The variable as.is controls this conversion. Its value is either a vector of logicals (values are recycled if necessary), or a vector of numeric or character indices which specify which columns should not be converted to factors.

Note: to suppress all conversions including those of numeric columns, set colClasses = "character".

http://stuff.mit.edu/afs/sipb/project/r-project/arch/i386_rhel3/lib/R/library/base/html/read.table.html

like image 38
I82Much Avatar answered Mar 11 '23 17:03

I82Much