我有以下df:
>df
cluster_no days_to_port channel
1 1 2.8 MMR
2 2 4.1 Spark Retail Stores
3 3 2.4 Spark Retail Stores
4 4 3.0 Spark Retail Stores
5 5 3.5 Spark Retail Stores
channel_outlet_name region
1 PIN Distributor Ltd (Activata) Auckland - Central
2 Spark - Auckland Int Airport Mangere
3 Spark - Auckland Int Airport Mangere
4 Spark - Auckland Int Airport Mangere
5 Spark - Auckland Int Airport Mangere
我正在尝试将其重新整形/转换为以下格式,以便我可以进行一些进一步的条件格式化和分析:(因此列是簇号,行是变量)
cluster_no 1 2 3 4 5
days_to_port
channel
channel_outlet_name
region
我可以在excel中使用pivot:
我想使用R来实现相同的目标。(df中的BTW数值是平均值,分类值是模式)
答案 0 :(得分:1)
df <- data.frame(cluster=1:5,
days_to_port=c(2.8,4.1,2.4,3.0,3.5),
channel=c('MMR','Spark Retail Stores','Spark Retail Stores','Spark Retail Stores','Spark Retail Stores'),
channel_outlet_name=c('PIN Distributor Ltd (Activata)','Spark - Auckland Int Airport','Spark - Auckland Int Airport','Spark - Auckland Int Airport','Spark - Auckland Int Airport'),
region=c('Auckland - Central','Mangere','Mangere','Mangere','Mangere'))
library(tidyr)
out <- df %>% gather(key=key,value=value,-cluster) %>%
spread(cluster,value)
out
key 1 2 3 4 5
1 channel MMR Spark Retail Stores Spark Retail Stores Spark Retail Stores Spark Retail Stores
2 channel_outlet_name PIN Distributor Ltd (Activata) Spark - Auckland Int Airport Spark - Auckland Int Airport Spark - Auckland Int Airport Spark - Auckland Int Airport
3 days_to_port 2.8 4.1 2.4 3 3.5
4 region Auckland - Central Mangere Mangere Mangere Mangere