File size: 3,509 Bytes
2f8aa81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#create_pop_table

OUTPUTDIR_data_wrangling <- here("data_wrangling")
# create output dir if it does not exist.
if (!dir.exists(OUTPUTDIR_data_wrangling)) {
  dir.create(OUTPUTDIR_data_wrangling)
}


#Glottolog df for ISO_639 merging
glottolog_df <-
  read_tsv("data_wrangling/glottolog_cldf_wide_df.tsv", col_types = cols()) %>%
  dplyr::select(
    Glottocode,
    Language_ID,
    "ISO_639" = ISO639P3code,
    Language_level_ID,
    level,
    Family_ID,
    Longitude,
    Latitude
  ) %>%
  mutate(Language_level_ID = if_else(is.na(Language_level_ID), Glottocode, Language_level_ID))  %>%
  mutate(Family_ID = ifelse(is.na(Family_ID), Language_level_ID, Family_ID)) %>%
  dplyr::select(
    Glottocode,
    Language_ID,
    ISO_639,
    Language_level_ID,
    level,
    Family_ID,
    Longitude,
    Latitude
  )


if (sample == "full") {
  data_ethnologue <-
    read_tsv("data_wrangling/ethnologue_pop_full.tsv")
}

if (sample == "reduced") {
  #double check if the file below needs to be changed
  data_ethnologue <-
    read_tsv("data_wrangling/ethnologue_pop_SM.tsv", show_col_types = F) %>%
    rename(L1_log10_st = L1_log10_scaled) %>%
    dplyr::select(ISO_639, Language_ID, L1_log10_st, L2_prop)
}

social_vars <-
  readxl::read_xlsx(
    "data/lang_endangerment_predictors.xlsx",
    sheet = "Supplementary data 1",
    skip = 1,
    col_types = "text",
    na = "NA"
  ) %>%
  left_join(glottolog_df, by = c("ISO" = "ISO_639")) %>%
  rename("ISO_639" = "ISO") %>%
  dplyr::select(
    Language_ID = Glottocode,
    ISO_639,
    official_status,
    language_of_education,
    bordering_language_richness
  ) %>%
  rename(Official = official_status) %>%
  #  naniar::replace_with_na(replace = list(L1_log10 = -Inf, L2_log10 = -Inf)) #removing for now
  dplyr::mutate(neighboring_languages = bordering_language_richness, Education =
                  language_of_education) %>%
  dplyr::mutate(neighboring_languages = as.numeric(neighboring_languages)) %>%
  #dplyr::mutate(neighboring_languages_log10 = log10(neighboring_languages+1)) %>%
  dplyr::mutate(neighboring_languages_st  = scale(neighboring_languages)[, 1]) %>%
  #dplyr::mutate(neighboring_languages_log10_st  = scale(neighboring_languages_log10)[,1]) %>%
  dplyr::select(Language_ID, Education, Official, neighboring_languages_st)

if (sample == "full") {
  social_vars  %>%
    left_join(data_ethnologue, by = c("Language_ID")) %>%
    dplyr::select(
      Language_ID,
      L1_log10_st,
      L1_log10,
      L2_prop,
      Education,
      Official,
      neighboring_languages_st
    ) %>%
    write_tsv(here(OUTPUTDIR_data_wrangling, "pop_full.tsv"))
}	else{
  social_vars  %>%
    left_join(data_ethnologue, by = c("Language_ID")) %>%
    dplyr::select(Language_ID,
                  L1_log10_st,
                  L2_prop,
                  Education,
                  Official,
                  neighboring_languages_st) %>%
    write_tsv(here(OUTPUTDIR_data_wrangling, "pop_reduced.tsv"))
}

glottolog_df_ISO <- glottolog_df %>% 
dplyr::select("Language_ID", "ISO_639")

if (sample == "reduced") {
  social_vars  %>%
    left_join(data_ethnologue, by = c("Language_ID")) %>%
    dplyr::select(Language_ID,
                  L1_log10_st,
                  L2_prop,
                  Education,
                  Official,
                  neighboring_languages_st) %>%
    left_join(glottolog_df_ISO,
              by = c("Language_ID")) %>%
    write_tsv(here(OUTPUTDIR_data_wrangling, "pop_reduced_with_ISO.tsv"))
}