Converting data frame into numeric sparseMatrix in R

Viewed 75

I have a data.frame with 3 columns. The structure of the data.fame is as below

str(data) 'data.frame': 76971772 obs. of 3 variables: $ V1: chr "XH104_AACGAGAGCTAAACTAGCCCTA" "XH104_AACGAGAGCTAAACTAGCCCTA" "XH104_AACGAGAGCTAAACTAGCCCTA" "XH104_AACGAGAGCTAAACTAGCCCTA" ... $ V2: chr "10:100175000-100180000" "10:101065000-101070000" "10:101550000-101555000" "10:101585000-101590000" ... $ V3: int 2 2 2 2 10 1 2 2 2 2 ...

I am trying to convert it into sparseMatrix such that the row name of sparseMatrix is data$V1 and the column name is data$V2. I am using the command given below to do that.

sparse.data <- with(data, sparseMatrix(i=as.numeric(V1), j=as.numeric(V2), x=V3, dimnames=list(levels(V1), levels(V2))))

I keep getting the this error.

Error in sparseMatrix(i = as.numeric(V1), j = as.numeric(V2), x = V3,  : 
  NA's in (i,j) are not allowed

I realized that when I use i=as.numeric(V1) in my command, all the values of V1 become NA.

Can someone suggest how can I solve this error?

0 Answers
Related