通过长和宽到长的混合重塑数据

Reshaping the data with mix of long and wide to long

我想这个问题可能很简单,请帮帮我。

我有这样的数据集:

enter image description here

我需要像下面这样重塑它:

enter image description here

提前致谢。

我建议根据 Location、City、CustomerId 和 partner 行的值为每个观察(计数)创建一个列名称。例如paste0(Location, City, CustomerId, partner, collapse = '/') 因此,使用 reshape2::melt 到 "melt" 数据帧到长格式。 然后,使用:

tidyr::separate(
  data, 
  "collapsedColName",  
  into = c("Location", "City", "CustomerId", "partner"),
  sep = "/"
  )

重新创建这些变量。