Search code examples
rdataframecategorical-data

Please missing data


Please, I need some help I am new in R and I am unable to analyze my data. I used this code:

Paises <- read.csv("~/Desktop/UN/Paises.csv") View(Paises)

```{R}
Datos = read.table("~/Desktop/UN/Paises.csv", header=T, sep="," , dec=".") 
str(Datos)

Then I do not know how to sum all 2010 or all 2020 I tried with colSums but it was not possible. I want to know the total population in 2010 and also know the total in 2010 from different regions or even different countries

enter image description here


Solution

  • Never use attach. Read Why is it not advisable to use attach() in R, and what should I use instead?

    Your title and description of problem (unable to analyse my data) is very vague. You need to tell what exactly you are trying to do. From your code attempt it seems you are trying to sum column 2010. However, from the screenshot I also see that you have spaces in numbers indicating that your columns is of type character (check sapply(datos, class)). We cannot take sum of character values so first turn the column values to numeric.

    Datos[-c(1:2)] <- lapply(Datos[-c(1:2)], as.numeric)
    

    Now if you want to sum X2010 column you can do :

    sum_2010 <- sum(Datos$X2010, na.rm = TRUE)
    sum_2010
    

    and if you want to sum all the numeric columns you can use colSums :

    sum_All <- colSums(Datos[-c(1:2)], na.rm = TRUE)
    sum_All