cambelot
cambelot

Reputation: 165

Read a column of data in R?

I am trying to read just one column of data in R. I know that the shortcut to do it is to do something like (assuming d1 is a data frame): d1[[3]] to read the third column. However, I'm just curious how would this simple function look like if you used read function instead? How would you make it a vector rather than a truncated data frame?

Upvotes: 0

Views: 534

Answers (1)

Whitebeard
Whitebeard

Reputation: 6203

Here's an example of reading just one column from a .csv file

dat <- data.frame(a = letters[1:3], b = LETTERS[1:3], c = 1:3, d = 3:1)
dat
  a b c d
1 a A 1 3
2 b B 2 2
3 c C 3 1

# write dat to a csv file
write.csv(dat,file="mydata.csv")

# scan the first row only from the file
firstrow <- scan("mydata.csv", sep=",", what=character(0), nlines=1)

# which position has the desired column (header = b in this cases)
col.pos <- match("b", firstrow)

# number of columns in data
nc <- length(firstrow)

# default of NA for desired column b; NULL for others
colClasses <- replace(rep("NULL", nc), col.pos, NA)

# read just column b
cols.b <- read.csv("mydata.csv", colClasses = colClasses)
cols.b
  b
1 A
2 B
3 C

The above reads in a data frame. If you want to read a vector,

cols.b <- read.csv("mydata.csv", colClasses = colClasses)[, 1]
cols.b
[1] A B C
Levels: A B C

Upvotes: 1

Related Questions