I have a wide table with only 1 row. Each column has a different name. I would like to combine 3 of the columns to form 1 "date" column and then transform the data to create a long table. Also my data table names will vary. For example I might have one table that has only 2 "ernMvx" and another table that has 20 "ernMvx" so I am using grep.
#data
dput(x)
structure(list(ernDate1 = "1/29/2019", ernDate2 = "11/1/2018",
ernDate3 = "7/31/2018", ernMv1 = 6.8335, ernMv2 = -6.6331,
ernMv3 = 5.891, ernStraPct1 = 6.8304, ernStraPct2 = 7.074,
ernStraPct3 = 5.2632), row.names = c(NA, -1L), class = "data.frame")
print(x)
ernDate1 ernDate2 ernDate3 ernMv1 ernMv2 ernMv3 ernStraPct1 ernStraPct2 ernStraPct3
1 1/29/2019 11/1/2018 7/31/2018 6.8335 -6.6331 5.891 6.8304 7.074 5.2632
date = x %>% select(grep("ernDate", names(x)))
ernMv = x %>% select(grep("ernMv",names(x)))
ernStraPct = x%>% select(grep("ernStra",names(x)))
new.data = as.data.frame(cbind(unlist(date), unlist(ernMv), unlist(ernStraPct)))
names(new.data) = c("date", "ernMv", "ernStraPct")
rownames(new.data) = c(1:length(new.data$date))
print(new.data)
date ernMv ernStraPct
1 1/29/2019 6.8335 6.8304
2 11/1/2018 -6.6331 7.074
3 7/31/2018 5.891 5.2632
This is my desired output, but it seems very tedious. Is there a way to do this quicker using a tidy verse function?I tried using reshape2::melt, but I seem to be having difficulties using it for a 1 row table. Thank you