1

I am trying to reshape my data using the melt function. I successfully did it once importing data from a csv file, but somehow when I tried to do to data from an xlsx file the error popped up.

Here are my data

PAR     T27 T25.5   T24 T22.5   T21 T19.5   T18 T16.5
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 0      0.06  0.06  0.06  0.06  0.06  0.06  0.06  0.06
2 6      1.44  1.67  1.63  1.6   1.6   1.42  1.54  1.47
3 18     3.78  4.94  4.88  4.68  4.72  3.87  4.34  4.08
4 35     5.06  7.63  7.7   7.05  7.04  5.23  5.7   5.2 
5 61     5.68  8.97  9.47  8.41  8.18  5.72  6.04  5.46
6 93     5.51  8.49  9.47  8.3   7.68  5.35  5.57  5.01
7 121    4.35  6.83  8.07  6.97  6.29  4.43  4.4   4.08
8 195    3.21  5.28  6.55  5.44  4.82  3.19  3.29  2.96
9 268    1.91  2.88  3.6   2.96  2.74  1.66  1.77  1.3

Here are my codes which worked for the csv file and not for the xlsx file:

library(readxl)
RLCx <- read_excel("RLC.xlsx")
RLCx$PAR=as.character(RLCx$PAR)

RLCxmelt<-melt(RLCx, id = c("PAR"))

Error message that get is:

Error in match.names(clabs, names(xi)) : 
names do not match previous names

The desired outcome (photo attached) is to have PAR column and T as independent variables.

Image: Reshaped data table

I also tried to use melt(RLCx, measure vars. = ("PAR") as well, but that didn't reshape the table in a sensible way. I'd appreciate if someone could help me out here. Thank you.

Rhyn
  • 135
  • 14
  • Does `melt(RLCx, id.vars = "PAR")` work? Alternatively, `tidyr::gather(RLCx, "T", "value", -PAR)`. – Axeman Jun 26 '18 at 07:58
  • can you provide a minimal reproducible example? – jyson Jun 26 '18 at 08:15
  • @Axeman Thank you. Your code `tidyr::gather` worked perfectly. But `id.vars` didn't work and I can't understand why. I do want to understand the language in this coding program so if you could shed some lights that would be great. – Rhyn Jun 27 '18 at 01:21
  • @user88911 You mean how I would like the data table to look like? (If so, it's in attached to the link in the post). – Rhyn Jun 27 '18 at 02:39

0 Answers0