somar colunar e ordenar por estado

ano regiao sg_estado id_cbo renda_mensal_total total_empregos total_estabelecimentos
2014 Norte AC 1112 206042.68 75 8
2014 Norte AC 1113 1921101.84 77 2
2014 Norte AC 1114 6304959.54 924 82
2014 Norte AC 1115 66968 12 4
2014 Norte AC 1141 37657.53 4 1
2014 Norte AC 1142 1448 1 1
2014 Norte AC 1144 19014.49 10 4
2014 Norte AC 1210 388895.93 49 37
2014 Norte AC 1221 4440.02 2 2
2014 Norte AM 3115 514245.27 141 53
2014 Norte AM 3116 28076.24 14 4
2014 Norte AM 3117 114739.85 115 80
2014 Norte AM 3121 4947031.61 1073 231
2014 Norte AM 3122 1736391.28 933 30
2014 Norte AM 3123 556787.42 235 69
2014 Norte AM 3131 8453901.03 1576 445

Prezados estou comeƧando agora estudar R e preciso da seguinte ajuda, como eu faƧo para Somar a coluna renda_mensal_total e ordenar por sg_estado.

agradeƧo a ajuda de todos

Hi @Ronnie! Welcome!

A small note: this is an English language forum, so you're more likely to get answers if you can post your questions in that language.

One good place to start learning about how to do this sort of thing is the Data Transformation chapter of the free book R for Data Science: https://r4ds.had.co.nz/transform.html

To give more specific advice, you'll need to explain exactly what sort of calculations you are trying to do here. It sounds like you want to summarize the table, calculating a sum of renda_mensal_total for certain groups. What variables do you want to group by? sg_estado, or something else?

Showing some code you have tried will be very helpful, even if the code doesn't work. Reading this FAQ will help you ask questions here in a way that gets you better, faster answers: FAQ: Tips for writing R-related questions

1 Like

The tidyverse packate dplyr has a number of tools for exactly this kind of thing,

Here's the Data Wrangling cheatsheet in Portuguese, many more located here: https://www.rstudio.com/resources/cheatsheets/

For example;

library(readr)
library(dplyr)

# had to edit your table allow it to be loaded by readr's read_delim: 
object <- "ano|regiao|sg_estado|id_cbo|renda_mensal_total|total_empregos|total_estabelecimentos|na
2014|Norte|AC|1112|206042.68|75|8|
2014|Norte|AC|1113|1921101.84|77|2|
2014|Norte|AC|1114|6304959.54|924|82|
2014|Norte|AC|1115|66968|12|4|
2014|Norte|AC|1141|37657.53|4|1|
2014|Norte|AC|1142|1448|1|1|
2014|Norte|AC|1144|19014.49|10|4|
2014|Norte|AC|1210|388895.93|49|37|
2014|Norte|AC|1221|4440.02|2|2|
2014|Norte|AM|3115|514245.27|141|53|
2014|Norte|AM|3116|28076.24|14|4|
2014|Norte|AM|3117|114739.85|115|80|
2014|Norte|AM|3121|4947031.61|1073|231|
2014|Norte|AM|3122|1736391.28|933|30|
2014|Norte|AM|3123|556787.42|235|69|
2014|Norte|AM|3131|8453901.03|1576|445|"
data_frame <- read_delim(object, delim = '|')

data_frame <- data_frame %>% 
  mutate(
    income_mensal_total =  total_empregos / 100 # you'd need correct logic here
  ) %>% 
  arrange(sg_estado)

data_frame
#> # A tibble: 16 x 9
#>      ano regiao sg_estado id_cbo renda_mensal_total total_empregos
#>    <int> <chr>  <chr>      <int>              <dbl>          <int>
#>  1  2014 Norte  AC          1112            206043.             75
#>  2  2014 Norte  AC          1113           1921102.             77
#>  3  2014 Norte  AC          1114           6304960.            924
#>  4  2014 Norte  AC          1115             66968              12
#>  5  2014 Norte  AC          1141             37658.              4
#>  6  2014 Norte  AC          1142              1448               1
#>  7  2014 Norte  AC          1144             19014.             10
#>  8  2014 Norte  AC          1210            388896.             49
#>  9  2014 Norte  AC          1221              4440.              2
#> 10  2014 Norte  AM          3115            514245.            141
#> 11  2014 Norte  AM          3116             28076.             14
#> 12  2014 Norte  AM          3117            114740.            115
#> 13  2014 Norte  AM          3121           4947032.           1073
#> 14  2014 Norte  AM          3122           1736391.            933
#> 15  2014 Norte  AM          3123            556787.            235
#> 16  2014 Norte  AM          3131           8453901.           1576
#> # ... with 3 more variables: total_estabelecimentos <int>, na <chr>,
#> #   income_mensal_total <dbl>

Created on 2018-11-19 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.