Saya memiliki data berikut di dataset saya:

Country <- c("ARG","BR","US","PY","SZ","SP","PT")
Gender <- c("MALE","FEMALE","MALE","MALE","FEMALE","MALE","FEMALE")

Saya ingin membuat tabel seperti ini:

| Country | n Male | % Male | n Female | % Female |
| --------| ------------------------------------- |
| ARG     | 1543   | 5,42%  |    1741  | 6,01%    |
| BR      | 1980   | 8,49%  |    1320  | 4,51%    |

Saya sudah mencoba dengan CrossTable tetapi tidak sama. Bagaimana saya bisa melakukannya?

r
1
AgusDam87 12 Mei 2021, 18:23

4 jawaban

Jawaban Terbaik

Di Basis R Anda dapat melakukan:

a <- table(Country, Gender)
cbind(a,proportions(a))[, c(1,3,2,4)]

    FEMALE    FEMALE MALE      MALE
ARG      0 0.0000000    1 0.1428571
BR       1 0.1428571    0 0.0000000
PT       1 0.1428571    0 0.0000000
PY       0 0.0000000    1 0.1428571
SP       0 0.0000000    1 0.1428571
SZ       1 0.1428571    0 0.0000000
US       0 0.0000000    1 0.1428571
1
Onyambu 12 Mei 2021, 16:51

Kita bisa menggunakan adorn_percentages dari janitor

library(janitor)
library(tibble)
tibble(Country, Gender) %>% 
   tabyl(Country, Gender) %>%
   adorn_percentages(denominator = c('all'))

-keluaran

# Country    FEMALE      MALE
#     ARG 0.0000000 0.1428571
#      BR 0.1428571 0.0000000
#      PT 0.1428571 0.0000000
#      PY 0.0000000 0.1428571
#      SP 0.0000000 0.1428571
#      SZ 0.1428571 0.0000000
#      US 0.0000000 0.1428571
1
akrun 12 Mei 2021, 17:54

Satu pendekatan dengan data.table:

library(data.table)

dt <- data.table( Country = c("ARG", "ARG", "BR", "US", "PY", "SZ", "SP", "PT"),
                  Gender  = c("MALE", "MALE", "FEMALE", "MALE", "MALE", "FEMALE", "MALE", "FEMALE"))

dt[ , .(n_male     = sum(Gender == "MALE"), 
        pct_male   = sum(Gender == "MALE") / nrow(dt), 
        n_female   = sum(Gender == "FEMALE"), 
        pct_female = sum(Gender == "FEMALE") / nrow(dt)),
   by = .(Country)]

#>    Country n_male pct_male n_female pct_female
#> 1:     ARG      2    0.250        0      0.000
#> 2:      BR      0    0.000        1      0.125
#> 3:      US      1    0.125        0      0.000
#> 4:      PY      1    0.125        0      0.000
#> 5:      SZ      0    0.000        1      0.125
#> 6:      SP      1    0.125        0      0.000
#> 7:      PT      0    0.000        1      0.125
1
DanY 12 Mei 2021, 16:49

Di sisi saya, saya melakukan kode ini:

Country <- c("ARG","BR","US","PY","SZ","SP","PT")
Gender <- c("MALE","FEMALE","MALE","MALE","FEMALE","MALE","FEMALE")
a= as.data.frame(cbind(Country, Gender))
m = table(a$Gender)['MALE']
f= table(a$Gender)['FEMALE']
res = matrix(ncol = 5)[-1,]
for(i in unique(a[,1])){
  b= a[a$Country==i,"Gender"]
  res=rbind(res, c(i, sum(b=='MALE'), sum(b=='MALE')/m*100,sum(b=='FEMALE'), sum(b=='FEMALE')/m*100))
}

Beri tahu saya jika itu berhasil

1
elielink 12 Mei 2021, 16:31