Welcome.
In R there are several ways to do what you are looking for. The most direct, using the base functions that are already installed with R, is to use the function table()
.
Let's say that your data.frame is called df
and the column you want the counts of is called Estados
. In that case you could see the counts asking R to make a table with the Estados
column of the data.frame df
. I'm going to create some data with that structure so you can see the example:
df <- data.frame(Estados = c (1, 2, 3, 3, 3, 4, 5, 5))
table (df$Estados) #Uso el signo $ para indicar la columna del data.frame que me interesa.
1 2 3 4 5
1 1 3 1 2
The console output has two lines, above the names and below the counts of each one.
It may be clearer if we directly use state names (in this case from Mexico)
nuevo_df <- data.frame(Estados =c ("Tlaxcala", "Querétaro", "Nayarit","Nayarit", "Querétaro", "Tlaxcala", "Chiapas", "Nayarit"))
table (nuevo_df$Estados)
Chiapas Nayarit Querétaro Tlaxcala
1 3 2 2
If you need your output to have the structure that you presented in your question, you can do it, but you should make sure you have installed the tidyverse
library, to use the functions group_by()
and 'count ().
#install.packages(tidyverse) #Sólo si no tienes instalado el paquete. Al tener un # detrás no se ejecutará.
library(tidiyverse) #Para disponer de las funciones del paquete ne tu entorno de trabajo.
df %>% #Primero llamo a los datos. Uso el símbolo %>% para conectar las operaciones.
group_by(Estados) %>% #Luego indico que quiero un grupo por cada valor único en Estados, podría agupar por más variables si las tuviera/quisiera.
tally() #tally cuenta cuantos elementos hay en cada grupo.
What comes back in console as follows:
# A tibble: 4 x 2
Estados n
<fct> <int>
1 Chiapas 1
2 Nayarit 3
3 Querétaro 2
4 Tlaxcala 2