R, importing as matrix data, calculates row sums but excludes headers

I am a student and working on a project and I cannot do the main thing in R and its very frustrating.

data <- read.xlsx("dataset.xlsx",1, header=1)
#Creating a matrix with headers.
matrix <- as.matrix(data, row.names=1, col.names=1)

Sum <- rowSums(matrix, na.rm = FALSE, dims = 1)

      

The data looks something like this:

    Point1  Point2   Point3
SP1  0      1        1
SP1  0      1        0
SP1  1      1        0
SP1  1      1        1
SP1  0      1        0

      

This data is located and imported into R using the xlsx package. After that, I want to calculate the sums of the rows and import them into a matrix to create something like this:

    Point1  Point2   Point3  Sums
SP1  0      1        1       2
SP1  0      1        0       1
SP1  1      1        0       2
SP1  1      1        1       3
SP1  0      1        0       1

      

However, I can't seem to get this to work. The code I am using keeps returning a non-numeric value. Obviously it is probably trying to calculate with the first row and column, which are the headers. For future purposes, Id as R always excludes the first row and column and treats them as headers and treats 0s and 1s as numeric.

Could you guys help me here?

Cheers jasper

+3


source to share


1 answer


Your matrix contains symbols and you are trying to sum them. So you first need to convert the submatrix you want to sum to numeric

(better to use class

here, not as.numeric

since it preserves the object type). To add a new column to a matrix you can use cbind

. Then you can update the code names.

Here's a one-liner:

`colnames<-`(cbind(matrix, rowSums(`class<-`(matrix[,-1], 'numeric'))),
             c(colnames(matrix), 'Sums'))

#           col1 col2 col3 Sums
#[1,] "Sp1" "1"  "0"  "0"  "1" 
#[2,] "Sp2" "1"  "0"  "0"  "1" 
#[3,] "Sp3" "1"  "1"  "0"  "2" 
#[4,] "Sp4" "0"  "1"  "1"  "2" 

      



Data

matrix = structure(c('Sp1', 'Sp2', 'Sp3', 'Sp4', 1, 1, 1, 0, 0, 0, 1, 1, 0, 
0, 0, 1), .Dim = c(4L, 4L), .Dimnames = list(NULL, c("", "col1", "col2", "col3")))

      

+2


source







All Articles