R that contains a column with values โ€‹โ€‹001000

I would like to read a file containing 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 get something like this:

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

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

+4
source share
4 answers

I was confused by the documentation, so I asked;) I managed to find it:

  read.table("file.txt", colClasses=c("character")) 
+2
source

Read the documentation for read.table() and learn how to select column types. You want the second column to be a character.

+2
source

It looks like you can pass the as.is argument to change whether read.table is trying to parse strings in values โ€‹โ€‹or save them as raw strings.

as.is the default behavior of read.table is the conversion of character variables (which are not converted to logical, numeric, or complex) into factors. The as.is variable controls this conversion. Its value is either a logical vector (values โ€‹โ€‹are returned if necessary), or a vector of numerical or symbolic indices that indicate which columns should not be converted to factors.

Note. To suppress all conversions, including number columns, set colClasses = "character".

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

+2
source

In general, I would answer with Dirk. But perhaps a short note helps someone who stumbles on this topic, looks at how to handle the less common data formats. Also see read.fwf from the utils package. This is really good if you have data stored without delimiters, as in some older databases.

That being said, in your particular case, I would probably go with read.table.

+2
source

Source: https://habr.com/ru/post/1338897/


All Articles