-4

I think the question might be simple, please help me with this.

I have a dataset like this:

enter image description here

and I need to reshape it like below:

enter image description here

Thanks in advance.

kiran u
  • 49
  • 6
  • 2
    Duplicate question (28 mins ago ...) you can found some solutions on this question [How to transform R data from long-ish to wide-ish](https://stackoverflow.com/questions/58909012/how-to-transform-r-data-from-long-ish-to-wide-ish). – dc37 Nov 18 '19 at 06:55
  • Hi dc37, I appreciate taking time in going through my question post. My dataset consists mix of wide and long. Pls help me transform to long – kiran u Nov 18 '19 at 06:59
  • 1
    You should provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your datasets. Based on your images, your initial dataset does not look like R data.frame – dc37 Nov 18 '19 at 07:01
  • The image 1 is the data I have and image 2 is the data I need to transform to. I need to do this in R for some automation purpose. I am not able to find any minimal code to do this in R. In excel, I can only do manual paste transpose function which is time consuming and not good to automate. – kiran u Nov 18 '19 at 09:32
  • Any help would be greatly appreciated – kiran u Nov 18 '19 at 09:32

1 Answers1

0

I would suggest creating a column name for each observation (count) based on the values from the Location, City, CustomerId and partner rows. e.g. paste0(Location, City, CustomerId, partner, collapse = '/') Consequently, use reshape2::melt to "melt" the dataframe to long format. Afterwards, use:

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

to recreate those variables.

Jeroen Colin
  • 345
  • 1
  • 6