簡體   English   中英

將R中的表轉換為單行

[英]Convert table in R into a single row

我想將具有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%

等等

這將使您非常接近:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM