Comp_Warrior
Comp_Warrior

Reputation: 179

Retain row names for subsets of csv data

I have some data in a csv file, which includes row names. I want to take a single column of the data, while retaining the row names. The csv file was produced in the following manner:

MAT <- matrix(nrow=5, ncol=2, c(1:10))
rownames(MAT) <- c("First","Second","Third","Fourth","Fifth")
write.csv(MAT, file='~/test.csv', row.names=TRUE) 

The matrix MAT is given below. Ultimately I want the first column of this matrix (after loading the csv file), with the row names intact.

       [,1] [,2]
First     1    6
Second    2    7
Third     3    8
Fourth    4    9
Fifth     5   10

If I now read the csv file,

MAT2 <- read.csv(file='~/test.csv')

MAT2 is given by

        X V1 V2
 1  First  1  6
 2 Second  2  7
 3  Third  3  8
 4 Fourth  4  9
 5  Fifth  5 10

The read.csv command seems to have created another row. In any case, if I do MAT3 <- MAT2[,2], I do not get a matrix like above. as.matrix(MAT2[,2]) does not retain the row names as I want.

Any ideas of how to proceed?

Upvotes: 0

Views: 673

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193527

Perhaps a better starting point is:

read.csv(file='~/test.csv', row.names = 1)
       V1 V2
First   1  6
Second  2  7
Third   3  8
Fourth  4  9
Fifth   5 10

You can also wrap this in as.matrix:

as.matrix(read.csv(file='~/test.csv', row.names = 1))

Compare their structures:

> str(read.csv(file='~/test.csv', row.names = 1))
'data.frame':   5 obs. of  2 variables:
 $ V1: int  1 2 3 4 5
 $ V2: int  6 7 8 9 10
> str(as.matrix(read.csv(file='~/test.csv', row.names = 1)))
 int [1:5, 1:2] 1 2 3 4 5 6 7 8 9 10
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:5] "First" "Second" "Third" "Fourth" ...
  ..$ : chr [1:2] "V1" "V2"

If all you are actually concerned about is how to extract a column while retaining the original structure, perhaps drop = FALSE is what you're after:

MAT2 <- as.matrix(read.csv(file='~/test.csv', row.names = 1))
#        V1 V2
# First   1  6
# Second  2  7
# Third   3  8
# Fourth  4  9
# Fifth   5 10
MAT2[, 2]
# First Second  Third Fourth  Fifth 
#     6      7      8      9     10 
MAT2[, 2, drop = FALSE]
#        V2
# First   6
# Second  7
# Third   8
# Fourth  9
# Fifth  10

Upvotes: 2

Related Questions