我如何重组R中的数据框
how can i restructure the data frame in R
我有以下 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
我正在尝试 reshape/pivot 将其转换为以下格式,以便我可以进行一些进一步的条件格式化和分析:(所以列是簇号,行是变量)
cluster_no 1 2 3 4 5
days_to_port
channel
channel_outlet_name
region
我可以在 excel 中使用 pivot 实现:
我想用 R 实现同样的效果。(顺便说一句,df 中的数值是平均值,分类值是众数)
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
我有以下 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
我正在尝试 reshape/pivot 将其转换为以下格式,以便我可以进行一些进一步的条件格式化和分析:(所以列是簇号,行是变量)
cluster_no 1 2 3 4 5
days_to_port
channel
channel_outlet_name
region
我可以在 excel 中使用 pivot 实现:
我想用 R 实现同样的效果。(顺便说一句,df 中的数值是平均值,分类值是众数)
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