0

I have a list in long format as follows:

name days N
A     1   80
A     2   90
B     1   70
B     3   85
B     5   100
C     2   80
C     5   95

I would like to obtain the following output:

days   1  2  3  4  5
A      80 90 NA NA NA
B      70 NA 85 NA 100
C      NA 80 NA NA 95

I tried the function reshape and obtained the error:

reshape(data, idvar = data[,1], timevar = [,3], direction = "wide")
Error in `[.data.frame`(data, , timevar) : undefined columns selected

`

  • This should help you ; Long to wide using `tidyr` : https://stackoverflow.com/questions/29146287/long-to-wide-data-with-tidyr – PKumar Mar 24 '21 at 14:40

2 Answers2

0

Here is a base R option using reshape

reshape(
  df,
  direction = "wide",
  idvar = "name",
  timevar = "days"
)

which gives

  name N.1 N.2 N.3 N.5
1    A  80  90  NA  NA
3    B  70  NA  85 100
6    C  NA  80  NA  95
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0
library(tidyr)

df %>% 
  pivot_wider(id_cols = name, 
              names_from = days,
              values_from = N)

To change the name of the first column you can do names(df)[1] <- "days" after you run the above code.

Output

  name    `1`   `2`   `3`   `5`
  <chr> <int> <int> <int> <int>
1 A        80    90    NA    NA
2 B        70    NA    85   100
3 C        NA    80    NA    95

Note: the output is a tibble object which does allow for column names to be numbers, but when accessing them you will need to use backticks. Generally, column names as numbers isn't advisable and is why in base R data.frame will fix column names that start with a number. The solution posted by @ThomasIsCoding outputs a data.frame object, which is why the columns have an "N." prefix.

LMc
  • 12,577
  • 3
  • 31
  • 43