基于 UNvotes 数据创建数据框

Creating Data frames based on UNvotes data

我正在尝试重现图像中显示的 data frame,它试图根据对你的 UNvotes 数据的投票行为对国家/地区进行分组。

我的代码:

df <- unvotes %>%
  # join with roll_calls for date column
  left_join(roll_calls %>% select(rcid, date)) %>%
  # convert date from double to date class
  mutate(date = lubridate::as_date(date)) %>%
  # filter votings from 2010 or later
  filter(lubridate::year(date) >= 2010) %>%
  # add number of observations per country as column
  add_count(country) %>%
  # filter countries with at least 900 votings
  filter(vote >= 900) %>%
  select(rcid, country, vote) %>%
  # if a country didn't vote on a particular roll call, add an observation with `vote == "abstain"`
  complete(rcid, country, fill = list(vote = "abstain")) %>%
  # kmeans requires numeric columns so we convert the answers to numeric
  mutate(vote = case_when(
    vote == "yes" ~ 1,
    vote == "no" ~ 0,
    TRUE ~ 0.5 # abstain
  )) %>%
  # reshape the data such that rows represent countries, columns represent votings, and cells represent how a country voted in a particular voting 
  pivot_wider(names_from = country, values_from = vote)

我得到的输出

Joining, by = "rcid"

想要data frame

不确定是因为加入还是其他原因。

dput

的输出
> dput(head(df))
structure(list(rcid = c(3, 4, 5, 6, 7, 8), Afghanistan = c(0.5, 
0.5, 0.5, 0.5, 0.5, 0.5), Albania = c(0.5, 0.5, 0.5, 0.5, 0.5, 
0.5), Algeria = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Andorra = c(0.5, 
0.5, 0.5, 0.5, 0.5, 0.5), Angola = c(0.5, 0.5, 0.5, 0.5, 0.5, 
0.5), `Antigua & Barbuda` = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 
    Argentina = c(1, 0, 0, 0.5, 1, 1), Armenia = c(0.5, 0.5, 
    0.5, 0.5, 0.5, 0.5), Australia = c(0, 1, 1, 1, 0, 1), Austria = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Azerbaijan = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Bahamas = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Bahrain = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Bangladesh = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Barbados = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Belarus = c(0, 
    1, 1, 0, 0, 1), Belgium = c(1, 0, 0, 0, 1, 1), Belize = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Benin = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Bhutan = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Bolivia = c(1, 
    0.5, 0, 0, 1, 1), `Bosnia & Herzegovina` = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), Botswana = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5
    ), Brazil = c(1, 0, 1, 0, 1, 1), Brunei = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), Bulgaria = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5
    ), `Burkina Faso` = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Burundi = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Cambodia = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Cameroon = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Canada = c(0, 
    0, 0, 0, 0, 1), `Cape Verde` = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), `Central African Republic` = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Chad = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Chile = c(1, 
    0, 0, 1, 0, 1), China = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 
    Colombia = c(1, 0, 0, 0.5, 0, 1), Comoros = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), `Congo - Brazzaville` = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), `Congo - Kinshasa` = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), `Costa Rica` = c(1, 0, 0, 0.5, 1, 1), `Côte d’Ivoire` = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Croatia = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Cuba = c(1, 0, 1, 1, 1, 1), Cyprus = c(0.5, 0.5, 
    0.5, 0.5, 0.5, 0.5), Czechia = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Czechoslovakia = c(1, 1, 0.5, 0, 0, 1), Denmark = c(0, 
    0.5, 0, 0, 0, 0.5), Djibouti = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Dominica = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), `Dominican Republic` = c(1, 
    0, 0, 0.5, 1, 1), Ecuador = c(1, 0, 1, 1, 1, 1), Egypt = c(0.5, 
    0, 1, 0.5, 1, 1), `El Salvador` = c(1, 0, 1, 0.5, 0.5, 0.5
    ), `Equatorial Guinea` = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 
    Eritrea = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Estonia = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Eswatini = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Ethiopia = c(1, 0.5, 0, 0.5, 0.5, 0.5), `Federal Republic of Germany` = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Fiji = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Finland = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), France = c(0, 
    0.5, 1, 0, 0, 1), Gabon = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 
    Gambia = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Georgia = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), `German Democratic Republic` = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Germany = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Ghana = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Greece = c(0, 
    0, 0, 0, 0.5, 1), Grenada = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5
    ), Guatemala = c(1, 0, 0, 0, 0.5, 1), Guinea = c(0.5, 0.5, 
    0.5, 0.5, 0.5, 0.5), `Guinea-Bissau` = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Guyana = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Haiti = c(1, 
    0, 0, 0.5, 1, 0.5), Honduras = c(1, 0, 1, 1, 1, 0.5), Hungary = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Iceland = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), India = c(0, 0, 0, 0, 0, 1), Indonesia = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Iran = c(1, 0.5, 1, 1, 1, 0.5), 
    Iraq = c(0.5, 0, 1, 0.5, 1, 1), Ireland = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), Israel = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 
    Italy = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Jamaica = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Japan = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Jordan = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Kazakhstan = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Kenya = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Kiribati = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Kuwait = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Kyrgyzstan = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Laos = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Latvia = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Lebanon = c(0.5, 0, 1, 1, 1, 1), 
    Lesotho = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Liberia = c(0, 
    0.5, 0, 0, 0.5, 1), Libya = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5
    ), Liechtenstein = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Lithuania = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Luxembourg = c(0, 0, 0, 0, 1, 1
    ), Madagascar = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Malawi = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Malaysia = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Maldives = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Mali = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Malta = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), `Marshall Islands` = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5
    ), Mauritania = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Mauritius = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Mexico = c(1, 0, 1, 1, 1, 1), `Micronesia (Federated States of)` = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Moldova = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Monaco = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Mongolia = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Montenegro = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Morocco = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Mozambique = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), `Myanmar (Burma)` = c(0.5, 0.5, 
    0.5, 0.5, 0.5, 0.5), Namibia = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Nauru = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Nepal = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Netherlands = c(0, 0, 1, 0, 0.5, 
    1), `New Zealand` = c(0, 1, 0, 0, 0, 1), Nicaragua = c(1, 
    0, 1, 1, 1, 0.5), Niger = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 
    Nigeria = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), `North Korea` = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), `North Macedonia` = c(0.5, 0.5, 
    0.5, 0.5, 0.5, 0.5), Norway = c(0, 1, 0, 0, 0, 0.5), Oman = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Pakistan = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Palau = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Panama = c(1, 
    0, 1, 0.5, 1, 0.5), `Papua New Guinea` = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), Paraguay = c(1, 0, 1, 0.5, 0.5, 0.5), Peru = c(1, 
    0, 0, 1, 1, 1), Philippines = c(1, 0, 1, 1, 0.5, 1), Poland = c(1, 
    1, 0, 0, 0, 1), Portugal = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5
    ), Qatar = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Romania = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Russia = c(0, 1, 1, 0, 0, 1), Rwanda = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Samoa = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), `San Marino` = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), `São Tomé & Príncipe` = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), `Saudi Arabia` = c(0.5, 0, 1, 0.5, 
    1, 0.5), Senegal = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Seychelles = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), `Sierra Leone` = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), Singapore = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5
    ), Slovakia = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Slovenia = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), `Solomon Islands` = c(0.5, 0.5, 
    0.5, 0.5, 0.5, 0.5), Somalia = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), `South Africa` = c(0, 0, 1, 0, 0, 1), `South Korea` = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), `South Sudan` = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), Spain = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 
    `Sri Lanka` = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), `St. Kitts & Nevis` = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), `St. Lucia` = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), `St. Vincent & Grenadines` = c(0.5, 0.5, 
    0.5, 0.5, 0.5, 0.5), Sudan = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5
    ), Suriname = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Sweden = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Switzerland = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), Syria = c(1, 0, 1, 0, 1, 1), Taiwan = c(0, 
    0.5, 1, 0, 0, 1), Tajikistan = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Tanzania = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Thailand = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), `Timor-Leste` = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), Togo = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Tonga = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), `Trinidad & Tobago` = c(0.5, 0.5, 
    0.5, 0.5, 0.5, 0.5), Tunisia = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Turkey = c(1, 0, 1, 0, 1, 1), Turkmenistan = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Tuvalu = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Uganda = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5), Ukraine = c(0, 
    1, 1, 0, 0, 1), `United Arab Emirates` = c(0.5, 0.5, 0.5, 
    0.5, 0.5, 0.5), `United Kingdom` = c(0, 0, 0, 0, 1, 1), `United States` = c(1, 
    0, 0, 0, 0, 0), Uruguay = c(1, 0.5, 1, 1, 1, 1), Uzbekistan = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Vanuatu = c(0.5, 0.5, 0.5, 0.5, 
    0.5, 0.5), Venezuela = c(1, 0, 1, 0, 1, 0.5), Vietnam = c(0.5, 
    0.5, 0.5, 0.5, 0.5, 0.5), Yemen = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), `Yemen Arab Republic` = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), `Yemen People's Republic` = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Yugoslavia = c(0, 1, 1, 0, 0, 1), Zambia = c(0.5, 0.5, 
    0.5, 0.5, 0.5, 0.5), Zanzibar = c(0.5, 0.5, 0.5, 0.5, 0.5, 
    0.5), Zimbabwe = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

你在找这个吗?将您的 dput 数据作为 df -

library(tidyr)

df %>%
  pivot_longer(cols = -rcid) %>%
  pivot_wider(names_from = rcid, values_from = value)

# A tibble: 200 x 7
#   name                `3`   `4`   `5`   `6`   `7`   `8`
#   <chr>             <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 Afghanistan         0.5   0.5   0.5   0.5   0.5   0.5
# 2 Albania             0.5   0.5   0.5   0.5   0.5   0.5
# 3 Algeria             0.5   0.5   0.5   0.5   0.5   0.5
# 4 Andorra             0.5   0.5   0.5   0.5   0.5   0.5
# 5 Angola              0.5   0.5   0.5   0.5   0.5   0.5
# 6 Antigua & Barbuda   0.5   0.5   0.5   0.5   0.5   0.5
# 7 Argentina           1     0     0     0.5   1     1  
# 8 Armenia             0.5   0.5   0.5   0.5   0.5   0.5
# 9 Australia           0     1     1     1     0     1  
#10 Austria             0.5   0.5   0.5   0.5   0.5   0.5
# … with 190 more rows