-4

i am a beginner in R. I have a data set called data_mat. it has only binary variables i.e. all variables have 0 or 1 as values. It look like this:

      Elderflower high low medium
 [1,]           0    1   0      0    
 [2,]           0    1   0      0    
 [3,]           0    1   0      0    
 [4,]           0    1   0      0

it is a numeric matrix. on adding 3 more columns (which are again binary in nature) into it using following code, it no more remains numeric:

data_mat<-cbind(data_mat,datam[,c(5:6,9)])

post executing above code, data now looks like this:

      Elderflower high low medium Sugar Chewy Content
 [1,] "0"         "1"  "0" "0"    "1"   "1"   "1"    
 [2,] "0"         "1"  "0" "0"    "0"   "0"   "1"    
 [3,] "0"         "1"  "0" "0"    "1"   "1"   "1"    
 [4,] "0"         "1"  "0" "0"    "0"   "0"   "1"   

in order to convert this matrix into numeric i ran following code:

data11<-matrix(as.numeric(unlist(data_mat,use.names = 
TRUE)),nrow=nrow(data_mat))

with this my data remains a numeric matrix but column names are gone. it now looks like this:

      [,59] [,60]
 [1,]     1     1
 [2,]     0     1
 [3,]     1     1
 [4,]     0     1

Kindly suggest how do i tackle this problem? Motive is to get numeric matrix with column names intact post adding new columns to existing numeric matrix.

user3459010
  • 101
  • 2
  • Your problem is that `datam` appears to be a character matrix. You should investigate why that is the case and fix it. – Roland Nov 14 '17 at 07:37
  • Take a look at this post: https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information – patL Nov 14 '17 at 07:42
  • 1
    Show your data with `dput()` or give the definition of your data! Please read [mcve] and https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jogo Nov 14 '17 at 07:43
  • @Roland yes datam is a character matrix. Please let me know how i add binary columns from this matrix to another numeric matrix in suc a way that resultant matrix is numeric with column names present – user3459010 Nov 14 '17 at 08:28
  • Then `datam` should **not** be a matrix. It should be a data.frame since in contrast to a matrix a data.frame can hold data of different types. You wouldn't have this issue then. – Roland Nov 14 '17 at 08:48

1 Answers1

0

Read your data with defined colClasses

data_mat <- read.table(path.to.data, header = TRUE, colClasses = "numeric")

This assures that your data is numeric and you can use cbind to add more columns the way you did. Of couse datam needs to be numeric, too. As a side effect, defining colClasses also accelerates the reading process.

smoff
  • 592
  • 5
  • 21
  • datam has both binary variables as well as string variables. I want to add only binary variable into my matrix. Because of string variables i will not be import my data like you have mentioned above. – user3459010 Nov 14 '17 at 08:44
  • then you have to define the class for every column like: `colClasses = c("numeric", "numeric", "character")` – smoff Nov 14 '17 at 08:50