Create a new row at the bottom of the database and add the column sums

I am reading a csv file from a working directory with the following code:

df <- read.csv("test1.csv", header = TRUE,skip =6, nrow =
length(count.fields("test1.csv")) - 12)


then changing the column names to the following code:

colnames(df) = c("type","date","v1","v2","v3","v4","v5","v6","v7","v8","v9","v10","v11","v12","v13","v14","v15","v16","v17","v18","v19","v20","v21","v22","v23","v24","total")


the size of my dataset is 365 rows x 24 columns and I am trying to calculate the column sums (3:27) and create a new row at the bottom of the database with the sums.

data.frame looks like this:

enter image description here

If I try the test with some sample data, then it works fine:

x <- data.frame(x1 = c(3:8, 1:2), x2 = c(4:1, 2:5),x3 = c(3:8, 1:2), x4 = c(4:1, 2:5))


x [9, (2: 3)] <- apply (x, 2, sum)

but when I try to use the csv file I am working with, the code I am using looks like this:

x[366,(3:27)] <- apply(df, 2, sum)


but it gives an error like this: "Error in FUN (newX [, i], ...): invalid 'type' (character) argument"

Can anyone please advise me how to resolve this?


source to share

1 answer

apply(df, 2, sum)

will try to sum all the columns, and if the columns type

and date

are of a type for which R has no method sum

, it will result in an error. You may try

df[366,(3:27)] <- colSums(df[,3:27], na.rm=TRUE)



To define numeric columns:

myNumCols <- which(unlist(lapply(df, is.numeric)))


And then

df[(nrow(df) + 1), myNumCols] <- colSums(df[, myNumCols], na.rm=TRUE)