I have a data like this
df_original<-structure(list(Mydata = c("AAA", "ABA", "CCC"), F1_100 = c(11448.6,
7611.5, 12257.1), F2_100 = c(2848.2, 2366.4, 2505.6), F3_100 = c(1257.8,
1967.7, 1681.7), F1_200M = c(13947.6, 8155.4, 13446.6), F2_200M = c(2743.3,
2349.6, 2490.1), F3_200M = c(1612.2, 2395.3, 2129.3), F1_165T = c(13561.5,
7628.7, 11687.4), F2_165T = c(2309.7, 2233.6, 2032), F3_165T = c(1754.1,
2526, 2132.5), F1_320Y = c(13368.2, 7301.7, 11176.5), F2_320T = c(3065.2,
2057.5, 1875.4), F1_320T = c(11319.4, 15751.6, 7778.5)), class = "data.frame", row.names = c(NA,
-3L))
I just give one example for the sake of simplicity
Lets look at the first raw (first four columns)
Mydata F1_100 F2_100 F3_100
AAA 11448.6 2848.2 1257.8
here I have AAA which has 3 values for 100 (3 columns). I want to rearrange it like this
Mydata F_100
AAA_F1 11448.6
AAA_F2 2848.2
AAA_F3 1257.8
I am trying to rearrange it which will look like the following
output_df <- structure(list(Mydata = c("AAA_F1", "AAA_F2", "AAA_F3", "ABA_F1",
"ABA_F2", "ABA_F3", "CCC_F1", "CCC_F2", "CCC_F3"), F_100 = c(11448.6,
2848.2, 1257.8, 7611.5, 2366.4, 1967.7, 12257.1, 2505.6, 1681.7
), F_200M = c(13947.6, 2743.3, 1612.2, 8155.4, 2349.6, 2395.3,
13446.6, 2490.1, 2129.3), F_165T = c(13561.5, 2309.7, 1754.1,
7628.7, 2233.6, 2526, 11687.4, 2032, 2132.5), F_320Y = c(13368.2,
NA, NA, 7301.7, NA, NA, 11176.5, NA, NA), F_320T = c(11319.4,
3065.2, NA, 15751.6, 2057.5, NA, 7778.5, 1875.4, NA)), class = "data.frame", row.names = c(NA,
-9L))
As it was shown above, the previous post just put everything in one column, does not solve my issue , for instance, look at this
reshape(df_original,
direction = "long",
varying = list(names(df_original)[3:13]),
v.names = "Value")
Or what @Gregor Thomas said
library(tidyverse)
df_original %>% select(Mydata, contains("_100")) %>% pivot_longer(cols = -Mydata)