将R中的表转换为单行

时间:2017-10-22 02:33:03

标签: r dataframe transpose reshape2

我想将包含n行和n列的数据框转换为单行,并附加列名和行名

例如,我希望将下表转换为单行

   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%

等等

1 个答案:

答案 0 :(得分:0)

这会让你非常接近:

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='_')