0

I have a dataframe that looks like this:

colnames<-c('Assay','Sample','Interference','0','24','48','168')
Assay<-c('1','2','3','4','5','6','7','8','9')
Sample<-c('S1.16S','B2','B5','B32','B36','B39','B47','B57','A15')
Interference<-c('N','Y','N','N','N','Y','N','Y','Y')
'0'<-c(14.337,24.21,14.97,15.51,14.64,28.83,13.63,21.08,20.26)
'24'<-c(NA,25.13,14.99,15.35,14.71,28.64,13.68,22.04,20.64)
'48'<-c(NA,25.32,15.00,15.97,14.64,28.69,13.87,NA,22.14)
'168'<-c(NA,24.7,15.09,17.31,14.67,NA,14.9,23.22,21.56)
d<-data.frame(Assay, Sample, Interference, 0,24,48,168, check.names=FALSE)

When using the reshape package to melt the data to long format, the values of columns 0, 24, 48, and 168 are changed to 1-4, like so:

library(reshape)
dlong<-melt(d, id.vars=c('Assay','Sample','Interference'), measure.vars=c('0','24','48','168'))
dlong$variable<-as.numeric(dlong$variable)
head(dlong)
#  Assay Sample Interference variable value
#1     1 S1.16S            N        1     0
#2     2     B2            Y        1     0
#3     3     B5            N        1     0
#4     4    B32            N        1     0
#5     5    B36            N        1     0
#6     6    B39            Y        1     0

How do I retain the numeric values as they are (0,24,48,168) in the column 'variable' without having to use additional code to convert them later? I need to run downstream correlation analyses on the full dataset, and this creates quite a lag in my processing time.

  • https://stackoverflow.com/questions/26391921/how-to-convert-entire-dataframe-to-numeric-while-preserving-decimals Following this previous post, I was able to correct the issue with this code `dlong$variable – carrie maryak Aug 25 '20 at 16:24

2 Answers2

1

I would suggest check your data structure. The issue you had is because of factors structure. To avoid the problem, add stringsAsFactors=F in your data.frame(). Next code can be useful for you:

library(reshape2)
#Data
colnames<-c('Assay','Sample','Interference','0','24','48','168')
Assay<-c('1','2','3','4','5','6','7','8','9')
Sample<-c('S1.16S','B2','B5','B32','B36','B39','B47','B57','A15')
Interference<-c('N','Y','N','N','N','Y','N','Y','Y')
'0'<-c(14.337,24.21,14.97,15.51,14.64,28.83,13.63,21.08,20.26)
'24'<-c(NA,25.13,14.99,15.35,14.71,28.64,13.68,22.04,20.64)
'48'<-c(NA,25.32,15.00,15.97,14.64,28.69,13.87,NA,22.14)
'168'<-c(NA,24.7,15.09,17.31,14.67,NA,14.9,23.22,21.56)
d<-data.frame(Assay, Sample, Interference, 0,24,48,168, check.names=FALSE,stringsAsFactors = F)
#Reshape
dlong<-melt(d, id.vars=c('Assay','Sample','Interference'), measure.vars=c('0','24','48','168'))

Output:

   Assay Sample Interference variable value
1      1 S1.16S            N        0     0
2      2     B2            Y        0     0
3      3     B5            N        0     0
4      4    B32            N        0     0
5      5    B36            N        0     0
6      6    B39            Y        0     0
7      7    B47            N        0     0
8      8    B57            Y        0     0
9      9    A15            Y        0     0
10     1 S1.16S            N       24    24
11     2     B2            Y       24    24
12     3     B5            N       24    24
13     4    B32            N       24    24
14     5    B36            N       24    24
15     6    B39            Y       24    24
16     7    B47            N       24    24
17     8    B57            Y       24    24
18     9    A15            Y       24    24
19     1 S1.16S            N       48    48
20     2     B2            Y       48    48
21     3     B5            N       48    48
22     4    B32            N       48    48
23     5    B36            N       48    48
24     6    B39            Y       48    48
25     7    B47            N       48    48
26     8    B57            Y       48    48
27     9    A15            Y       48    48
28     1 S1.16S            N      168   168
29     2     B2            Y      168   168
30     3     B5            N      168   168
31     4    B32            N      168   168
32     5    B36            N      168   168
33     6    B39            Y      168   168
34     7    B47            N      168   168
35     8    B57            Y      168   168
36     9    A15            Y      168   168
Duck
  • 37,428
  • 12
  • 34
  • 70
0

If we use R version >= 4.0.0, the default option is stringsAsFactors = FALSE, so we don't have to change in data.frame construction

library(dplyr)
library(tidyr)
d %>% 
    pivot_longer(cols = `0`:`168`)
akrun
  • 674,427
  • 24
  • 381
  • 486