将 R 中的 table 转换为单行

Convert table in R into a single row

我想将一个包含 n 行 n 列的数据框转换为带有列名和行名的单行

例如,我希望将以下 table 转换为一行

   Country         Percentage
   United States    97.89%
   United Kingdom   0.65%
   Switzerland      0.50%
   Ireland          0.48%
   Singapore        0.45%
   Hong Kong        0.03%

像这样

Country_United States_Percentage    Country_United Kingdom_Percentage
97.89%                                       0.65%

以此类推

这会让你非常接近:

library(tidyverse)
df <- tribble(
  ~Country, ~Percentage, 
  "United States", 97.89,
  "United Kingdom", 0.65,
  "Switzerland", 0.5,
  "Ireland", 0.48,
  "Singapore", 0.45,
  "Hong Kong", 0.03
)

spread(df, Country, Percentage, sep='_')