Reputation: 11
I need to get the sum of my data from the rows and columns. I uploaded my data in csv and then removed NA to replace them with zeros. I just can’t get my data to read as integers and the sum it up.
data<-read.csv("DataSet.2.csv",header=FALSE)
mode(data)
[1] "list"
data[is.na(data)]=0
data
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
1 Var_1 Var_2 Var_3 Var_4 Var_5 Var_6 Var_7 Var_8 Var_9 Var_10
2 Crow 8 8 0 3 2 4 4 44 0 23
3 Mouse 2 0 5 4 2 6 36 636 2 2
4 Boar 15 113 48 36 15 66 14 0 2 23
5 Plain 8 17 164 14 91 0 6 10 6 32
6 Silver.Carp 3 1 0 6 7 0 35 35 0 432
7 Dog 1 0 27 0 0 11 0 0 7 43
8 Bingo 2 3 1 15 1 21 0 0 1 0
9 Chrysalis 1 0 2 0 47 0 0 0 7 3
10 Apple 2 0 3 0 0 0 0 0 5 4
11 Cork 3 0 1 0 461 8 2305 15 0 2
12 Ant 11 0 2 0 0 0 0 91 4 0
13 Cat.Claw 2 22 1 110 2 7 10 7 0 0
14 Aardvark 3 1 0 5 25 30 125 0 5 4
15 Carriage 0 3 3 15 0 533 0 1 7 3
16 Airplane 3 2 1 10 0 28 0 47 7 1
17 Clipper 2 1 2 5 0 507 0 0 23 2
18 Armadillo 3 2 4 11 24 0 2 10 3322 0
19 Cork 3 3 1 9 461 88 2305 15 233 3
20 Colt 3 4 1 10 4902 0 0 1 4322 111
21 Cat 3 22 2 220 3 11 10 7 2333 22
V12
1 Var_11
2 15
3 4
4 13
5 3
6 312
7 1
8 22
9 12
10 0
11 0
12 23
13 32
14 44
15 43
16 2
17 33
18 2
19 3
20 55
21 3
#When I use as.numeric I am getting an error
data2<-as.numeric(data) Error: 'list' object cannot be coerced to type 'double'
Upvotes: 1
Views: 61
Reputation: 1473
It looks like your .csv file contains a header ('Var_1', 'Var_2', etc.) but you are specifying header=FALSE when you load the data, so those strings are being interpreted as data values. Additionally, it looks like your first column represents row names for your dataset. You can specify this via the row.names
argument.
Instead, load the data using:
data <- read.csv("DataSet.2.csv", header=TRUE, row.names = 1)
Once the data is loaded you can get the column and row sums via the functions colSums()
and rowSums()
, respectively. Additionally, if you are replacing the NA values with 0s just for the computation of the sums, you can skip that skip by setting the parameter, na.rm = TRUE
within colSum()
and rowSums()
. This will remove the NA values from the collection of the sums. For example:
data <- read.csv("DataSet.2.csv", header=TRUE, row.names = 1)
row_sum <- rowSums(data, na.rm = TRUE)
col_sum <- colSums(data, na.rm = TRUE)
Upvotes: 2