Sum of 2 date.frame columns

Asked

Viewed 28 times

-2

I need to add two columns of a date frame..

Example:

inserir a descrição da imagem aqui

Preiso add all rows of the columns rural and urban.

I tried the apply and rowsum codes and all gave error.

Error apply: apply(educa[, 3:4], 2, sum) Error in FUN(newX[, i], ...) 'invalid type' (Character) of argument

rowsum error: rowSums(educates[,3:4]) Error in rowSums(educa[, 3:4]) 'x' must be numerical

Educ["State Enrollment"] = rowSums(educa[,3:4])

  • 4

    Welcome to Stackoverflow! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please take a look at this link (mainly in the use of function dput) and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

1 answer

0

You have to convert to numeric before adding.

But first see with str(educa[3:4]) the class of columns:
If the columns of educa[3:4] are of class "factor"

educa[3:4] <- lapply(educa[3:4], function(x) as.numeric(as.character(x)))

If the columns of educa[3:4] are of class "character"

educa[3:4] <- lapply(educa[3:4], as.numeric)

Then just add up the columns.

rowSums(educa[3:4])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.