1

I have a data like this

df<- structure(list(rn = structure(1:8, .Label = c("A", "B", "C", 
"D", "E", "F", "G", "H"), class = "factor"), WS1.Median = c(0.000621, 
0.000381, 0.00247, 5.94e-05, 0.000202, 0.00177, 0.000281, 0.00121
), WS1.SD = c(0.000127453, 9.23e-05, 0.000125033, 5.91e-06, 6.07e-05, 
0.000531633, 2.62e-05, 0.000277849), Wmp1.Median = c(0.000777, 
0.00177, 0.00199, 4.12e-05, 0.000167, 0.00255, 3e-04, 0.0019), 
    Wmp1.SD = c(0.000107802, 0.000120554, 0.000410528, 8.98e-06, 
    8.11e-05, 0.001238924, 2.27e-05, 0.000903124), WTb1.Median = c(0.000574, 
    0.002, 0.00287, 4.44e-05, 0.000238, 0.000253, 0.000304, 0.00374
    ), WTb1.SD = c(0.001048659, 0.000914697, 0.000450444, 1.11e-05, 
    2.91e-05, 0.000401285, 8.41e-05, 0.002588796), Wf1.Median = c(0.000537, 
    0.000457, 0.00224, 5.68e-05, 0.000242, 0.000934, 0.000284, 
    0.00153), Wf1.SD = c(9.32e-05, 0.000167046, 0.000310483, 
    1.16e-05, 4.07e-05, 9.12e-05, 5.28e-05, 8.89e-05)), class = "data.frame", row.names = c(NA, 
-8L))

I am trying to reshape it and have specific info and I do the following

df %>% gather(Var, Value, -rn) %>%
  arrange(rn) %>% group_by(rn) %>% mutate(n=row_number()) 

I want the output to at least have the following info look like this

rn  key         x     y      place    sd   
A   WS1.Median  1  0.000621 WS1.SD  0.000127453
A   Wmp1.Median 1  0.000777 Wmp1.SD 0.000107802
A   WTb1.Median 1  0.000574 WTb1.SD 0.001048659
A   Wf1.Median  1  0.000537 Wf1.SD  9.32E-05
Learner
  • 757
  • 3
  • 15
  • I think instead of 'key', 'place' columns, a better approach is one column 'key' and the column names for 'y' and 'sd' as 'median' and 'SD' – akrun Nov 18 '19 at 22:16
  • 2
    Does this answer your question? [Reshaping multiple sets of measurement columns (wide format) into single columns (long format)](https://stackoverflow.com/questions/12466493/reshaping-multiple-sets-of-measurement-columns-wide-format-into-single-columns) – M-- Nov 18 '19 at 22:33
  • 1
    Learner, I cv'd this as a dupe, but did not downvoted your question. I may be wrong but I had your profile open, with no downvotes, I got a downvote on one of my questions, and now you have one downvote. You are entitled to downvote, as the privilege has been awarded to you, but please consider using it in a proper manner. Just to clarify, I only downvote based on the quality of the question, which this one with the reproducible example and good formatting, qualifies for an upvote. Cheers. +1 – M-- Nov 18 '19 at 22:49
  • @M-- I have read your post which is great!! I have trying to add row numbers to the reshape but seems like this `pivot_longer` does not do the trick – Learner Nov 19 '19 at 17:02
  • 1
    ```library(dplyr); library(tidyr); library(tibble); df %>% rownames_to_column("x") %>% pivot_longer(cols = -c(rn,x), names_to = c( "key", ".value"), names_sep="[.]")``` – M-- Nov 19 '19 at 17:05

1 Answers1

3

We can use pivot_longer

library(dplyr)
library(tidyr)

df %>%      
  pivot_longer(cols = -rn, names_to = c( "key", ".value"), names_sep="[.]") %>%
  group_by(rn) %>%
  mutate(x = row_number())
akrun
  • 874,273
  • 37
  • 540
  • 662