Skip to content

use read.table for a partial reading

4 messages · Covelli Paolo, PIKAL Petr, Robert Baer +1 more

#
Hi everyone,

I've got a matrix data with 20 variables (V1, V2, V3, ...) and 215  
rows (observations). I'm interested to read only the first and second  
variables using "read.table" function. How can I do?

Thanks in advance.

Paolo.
#
Hi

what about

some.data <- read.table(....)[ ,1:2]

Regards
Petr


r-help-bounces at r-project.org napsal dne 08.04.2010 16:05:39:
http://www.R-project.org/posting-guide.html
2 days later
#
----- Original Message ----- 
From: "Covelli Paolo" <pcovelli at tele2.it>
To: <r-help at r-project.org>
Sent: Thursday, April 08, 2010 9:05 AM
Subject: [R] use read.table for a partial reading
You do not provide a reproducible example (see 
http://www.R-project.org/posting-guide.html), so my comments are somewhat of 
a guess.

Typically, when you read in data from a file in R, you read it into a 'data 
frame' not a matrix.
(To earn more 
http://cran.r-project.org/doc/manuals/R-intro.html#Lists-and-data-frames)

This is how you would get your data (the dataframe) in the form you desire:
dat <- read.table("mydata.txt", header = TRUE)
dat2 <- dat[,1:2]  # contains just the first two variables, V1 and V2
#
Try this also:

your_data <- read.table('your_file.txt', colClasses = c('character',
'character', rep(NULL, 18)))
On Thu, Apr 8, 2010 at 11:05 AM, Covelli Paolo <pcovelli at tele2.it> wrote: