将R中的表转换为单行
问题描述:
我想将具有n行和n列的数据帧转换为单行,并添加了列名和行名称将R中的表转换为单行
例如,我想将下表转换为单行
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%
等等
答
这个Wi会让你非常接近:
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='_')
+0
谢谢,这非常有用 –
'dplyr :: spread()'?可能会帮助 –