0

my data look like this : 1-pre 2-mid 3-post

id<-c(1,2,3,4,5,6,7,8,9)
type<-c(1,2,2,1,2,1,1,1,2)  #is the factor level 1 and 2
k1<-c(30.7,20.3,3.4,22,11.6,29.5,15.4,2.7,2.1)
k2<-c(13.4,11,1.4,9.6,17.3,27.1,9.3,5.3,4.7)
k3<-c(1.2,10,4.7,2.6,13.4,14.8,8.8,0.7,4.6)
m1<-c(12    ,12.6,  1.6,    6.2,    10.5,   8.6,    15.3,   1.5,    1.3)
m2<-c(6.6   ,10.5,  0.5,    2.7,    5.2,    8.5,    9.6,    0.6,    4.3)
m3<-c(2,11, 2.6,0.4,7.2,9.4,12.3,0.3,2)
df<-data.frame(type,k1,k2,k3,m1,m2,m3,id)
df[,1]<-as.factor(df[,1])

my new data frame look like this

    type id time score
1     1  1   k1  30.7
2     1  1   k2  13.4
3     1  1   k3   1.2
4     1  1   m1  12.0
5     1  1   m2   6.6
6     1  1   m3   2.0
7     2  2   k1  20.3
8     2  2   k2  11.0

this command i use

dflong<-tidyr::gather(df,key=time, value=score, k1:k3,m1:m3)%>% arrange(id)

i want one column for period (pre,mid,post) and other column for value of m and k variable

4

2 回答 2

0

In base R, you could use reshape function,. by specifying the varying columns ie 2:7 and then the separator for the varying columns to be "". if the columns had been named as k.1, k.2... then the separator would have been .

  reshape(df,2:7,dir="long",sep="")

    type id time    k    m
1.1    1  1    1 30.7 12.0
2.1    2  2    1 20.3 12.6
3.1    2  3    1  3.4  1.6
4.1    1  4    1 22.0  6.2
5.1    2  5    1 11.6 10.5
6.1    1  6    1 29.5  8.6
7.1    1  7    1 15.4 15.3
8.1    1  8    1  2.7  1.5
9.1    2  9    1  2.1  1.3
1.2    1  1    2 13.4  6.6
2.2    2  2    2 11.0 10.5
3.2    2  3    2  1.4  0.5
4.2    1  4    2  9.6  2.7
5.2    2  5    2 17.3  5.2
6.2    1  6    2 27.1  8.5
7.2    1  7    2  9.3  9.6
8.2    1  8    2  5.3  0.6
9.2    2  9    2  4.7  4.3
1.3    1  1    3  1.2  2.0
2.3    2  2    3 10.0 11.0
3.3    2  3    3  4.7  2.6
4.3    1  4    3  2.6  0.4
5.3    2  5    3 13.4  7.2
6.3    1  6    3 14.8  9.4
7.3    1  7    3  8.8 12.3
8.3    1  8    3  0.7  0.3
9.3    2  9    3  4.6  2.0
于 2019-10-21T15:03:39.710 回答
0

Onyambu's solution is perfect but I thought I'd give this a shot with tidyr's new pivot function:

library(tidyverse)

df %>%
  pivot_longer(
    cols = k1:m3,
    names_sep = 1,
    names_to = c("mk", "time")
  ) %>%
  pivot_wider(
    id_cols = c(type, id, time),
    names_from = mk,
    values_from = value
  )
#> # A tibble: 27 x 5
#>    type     id time      k     m
#>    <fct> <dbl> <chr> <dbl> <dbl>
#>  1 1         1 1      30.7  12  
#>  2 1         1 2      13.4   6.6
#>  3 1         1 3       1.2   2  
#>  4 2         2 1      20.3  12.6
#>  5 2         2 2      11    10.5
#>  6 2         2 3      10    11  
#>  7 2         3 1       3.4   1.6
#>  8 2         3 2       1.4   0.5
#>  9 2         3 3       4.7   2.6
#> 10 1         4 1      22     6.2
#> # ... with 17 more rows

Data:

id <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
type <- c(1, 2, 2, 1, 2, 1, 1, 1, 2) # is the factor level 1 and 2
k1 <- c(30.7, 20.3, 3.4, 22, 11.6, 29.5, 15.4, 2.7, 2.1)
k2 <- c(13.4, 11, 1.4, 9.6, 17.3, 27.1, 9.3, 5.3, 4.7)
k3 <- c(1.2, 10, 4.7, 2.6, 13.4, 14.8, 8.8, 0.7, 4.6)
m1 <- c(12, 12.6, 1.6, 6.2, 10.5, 8.6, 15.3, 1.5, 1.3)
m2 <- c(6.6, 10.5, 0.5, 2.7, 5.2, 8.5, 9.6, 0.6, 4.3)
m3 <- c(2, 11, 2.6, 0.4, 7.2, 9.4, 12.3, 0.3, 2)
df <- data.frame(type, k1, k2, k3, m1, m2, m3, id)
df[, 1] <- as.factor(df[, 1])
于 2019-10-21T15:55:56.797 回答