The overall goal:
I have a tibble with a column of country names, a column that contains either the characters "Military" or "Economic", and a column of numbers (that were imported as characters). I need to aggregate the military spending for each country and the economic spending for each country.
My strategy:
I imported the data with
data <- read_excel("/path/name.xlsx")
which now makes data
a tibble, but all the values are automatically characters. To make the numeric columns summable, I write
data[,8] <- sapply(data[,8], as.numeric)
Seems to work, not sure if it's the best want to accomplish the goal. Anyway, now I want to get the factors of the country column, so that I can loop over every country name, and then loop over every row of the tibble, aggregating the military spending, and so on.
Question:
However,
levels(data[,3])
returns an error. So is there a way to get the levels without looping over the rows to collect them into a vector, and then going back over the rows trying to collect the military spending sum?
col_types
) as an argument toread_excel
readxl.tidyverse.org/articles/cell-and-column-types.html. You can also access columns withdata$your_column_name
.data[,N]
is pretty hard to read. – Jack Brookessapply
. Or "loop over" as per your final comment. Most functions in R are vectorized, so no need for loops. – Jack Brookes