简体   繁体   中英

how to transpose a dataset from wide format to long format in R

The dataset is similar to the following:

ID SEX bloodpressure1 bloodpressure2 bloodpressure3 weight1 weight2 weight3
1   1     90              100           NA            100     105     112
2   0      101            120          115            140     NA     150

*There are over 200 variables

I want the output to be as following:

ID SEX n bloodpressure weight
1   1  1      90        100
1   1  2      100       105
1   1  3      NA        112
2   0  1      101       140
2   0  2      120       NA
2   0  3      115       150

I tried solutions offered in this link: Using Reshape from wide to long in R but since the variable names in my dataset does not have '_' between the letter and the number, I don't how to separate the column name to make it work.

Thank you in advance for any help!

Using tidyr::pivot_longer :

tidyr::pivot_longer(df, cols = -c(ID, SEX), 
                   names_to = c('.value', 'n'), 
                   names_pattern = '(.*)(\\d+)')

# A tibble: 6 x 5
#     ID   SEX n     bloodpressure weight
#  <int> <int> <chr>         <int>  <int>
#1     1     1 1                90    100
#2     1     1 2               100    105
#3     1     1 3                NA    112
#4     2     0 1               101    140
#5     2     0 2               120     NA
#6     2     0 3               115    150

There are lots of examples on SO about reshaping. Probably the most common question. All you have to do is search.

reshape(data, direction="long", idvar="ID", varying=3:8, sep="")

    ID SEX time bloodpressure weight
1.1  1   1    1            90    100
2.1  2   0    1           101    140
1.2  1   1    2           100    105
2.2  2   0    2           120     NA
1.3  1   1    3            NA    112
2.3  2   0    3           115    150

The help page of reshape clearly explains the use of the sep argument. You can also try using tidy's pivot_longer function. Read the docs. ;)


Data:

data <- read.table(text="ID SEX bloodpressure1 bloodpressure2 bloodpressure3 weight1 weight2 weight3
1   1     90              100           NA            100     105     112
2   0      101            120          115            140     NA     150", header=TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM