问题
I am trying to convert my data from wide to long. I have an id variable and 131 columns of time data. All columns are integer format. The data was imported in csv format.
> class(ECGHR)
[1] "tbl_df" "tbl" "data.frame"
Here are the first 6 rows of data for 22 time-varying variables:
> head (ECGHR)
# A tibble: 6 × 132
id T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 2003 70 65 69 71 68 74 67 65 60 66 59 99 74 73 87 94 88 88 98 73 72 63 79
2 2004 98 105 85 87 83 83 77 71 75 73 73 71 87 74 79 75 72 71 71 68 86 72 73
3 2008 93 92 91 90 93 88 89 83 93 96 95 94 89 91 87 93 88 100 93 92 89 93 83
4 2010 71 69 74 71 74 68 74 68 78 75 81 72 74 77 74 70 64 68 65 69 66 73 75
5 2018 90 86 93 91 86 82 97 95 95 83 79 88 78 92 86 94 83 79 96 91 72 78 80
6 2019 60 58 59 58 66 68 73 77 58 110 56 56 53 49 73 56 45 50 46 45 46 54 50
Here is my reshape code:
ECGHR_long=reshape(ECGHR, varying=c(2:132), direction="long", idvar="id", sep="")
Here is the warning message:
Error in 'row.names<-.data.frame'('tmp', value = paste(d[, idvar], times[1L], : invalid 'row.names' length In addition: Warning message: Setting row names on a tibble is deprecated.
I am used to working with data frames and I can't work out why it won't reshape (I'm new to R). Do I need to convert it to a dat frame? I'd be very grateful for any insights!
回答1:
To use the reshape
function in the base of R try this -- we have omitted idvar="id"
since it can figure that one out itself but it won't hurt if you add it in.
reshape(as.data.frame(ECGHR), dir = "long", varying = list(2:ncol(ECGHR)),
times = names(ECGHR)[-1])
## id time T0
## 2003.T0 2003 T0 70
## 2004.T0 2004 T0 98
## 2008.T0 2008 T0 93
## 2010.T0 2010 T0 71
## 2018.T0 2018 T0 90
## 2019.T0 2019 T0 60
## etc.
or using the reshape2 package:
library(reshape2)
melt(ECGHR, id.vars = 1)
## id variable value
## 1 2003 T0 70
## 2 2004 T0 98
## 3 2008 T0 93
## 4 2010 T0 71
## 5 2018 T0 90
## 6 2019 T0 60
Note: We used this input:
Lines <- "id T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22
1 2003 70 65 69 71 68 74 67 65 60 66 59 99 74 73 87 94 88 88 98 73 72 63 79
2 2004 98 105 85 87 83 83 77 71 75 73 73 71 87 74 79 75 72 71 71 68 86 72 73
3 2008 93 92 91 90 93 88 89 83 93 96 95 94 89 91 87 93 88 100 93 92 89 93 83
4 2010 71 69 74 71 74 68 74 68 78 75 81 72 74 77 74 70 64 68 65 69 66 73 75
5 2018 90 86 93 91 86 82 97 95 95 83 79 88 78 92 86 94 83 79 96 91 72 78 80
6 2019 60 58 59 58 66 68 73 77 58 110 56 56 53 49 73 56 45 50 46 45 46 54 50"
ECGHR <- read.table(text = Lines, header = TRUE)
来源:https://stackoverflow.com/questions/43228109/problems-converting-from-wide-to-long-using-reshape-tibble-issue