Change data form from long to wide with condensed columns

Say that I have (fake) patient data from their visits:

## Create a fake dataframe  
foo <- data.frame(PatientNumber=c(11,11,11,22,22,33,33,33,44,55,55), 
      VisitDate=c("11/03/07","11/03/07","11/20/07","12/20/08", 
      "12/30/09","09/20/12","09/20/12","10/25/07","05/09/08","06/09/13","06/09/13"), 
       ICD9=c(10,15,10,30,30,25,60,25,14,40,13))

What gives:

   PatientNumber VisitDate ICD9
1             11  11/03/07   10
2             11  11/03/07   15
3             11  11/20/07   10
4             22  12/20/08   30
5             22  12/30/09   30
6             33  09/20/12   25
7             33  09/20/12   60
8             33  10/25/07   25
9             44  05/09/08   14
10            55  06/09/13   40
11            55  06/09/13   13

I would like to have a unique row for each patient at the time of the visit. If the patient has several codes for the date, I would like a new column for all ICD codes to be indicated during this visit. Here's what it looks like:

WhatIWant <- data.frame(PatientNumber=c(11,11,22,22,33,33,44,55), 
                    VisitDate=c("11/03/07", "11/20/07", "12/20/08", "12/30/09", "09/20/12","10/25/07","05/09/08","06/09/13"), 
                    ICD9_1=c(10,10,30,30,25,25,14,40), 
                    ICD9_2= c(15,NA,NA,NA,60,NA,NA,13))




> WhatIWant
  PatientNumber VisitDate ICD9_1 ICD9_2
1            11  11/03/07     10     15
2            11  11/20/07     10     NA
3            22  12/20/08     30     NA
4            22  12/30/09     30     NA
5            33  09/20/12     25     60
6            33  10/25/07     25     NA
7            44  05/09/08     14     NA
8            55  06/09/13     40     13

I tried changing the form, but it seems that all ICD9 columns are added to the column and add value to the column if they have a value or not (as shown below). I get something like 200 columns, I would like only 3 (the maximum number of codes per patient per visit in the data set, i.e. ICD9_1, ICD9_2, ICD9_3).

test <- reshape(foo, idvar = c("VisitDate"), timevar = c("PatientNumber"), direction = "wide")

> test
    VisitDate ICD9.11 ICD9.22 ICD9.33 ICD9.44 ICD9.55
1  0007-11-03      10      NA      NA      NA      NA
3  0007-11-20      10      NA      NA      NA      NA
4  0008-12-20      NA      30      NA      NA      NA
5  0009-12-30      NA      30      NA      NA      NA
6  0012-09-20      NA      NA      25      NA      NA
8  0007-10-25      NA      NA      25      NA      NA
9  0008-05-09      NA      NA      NA      14      NA
10 0013-06-09      NA      NA      NA      NA      40

, , , , , .

!

+4
3

reshape , . ave:

foo$time <- with(foo, ave(rep(1, nrow(foo)), 
                          PatientNumber, VisitDate, 
                          FUN = seq_along))

reshape :

reshape(foo, direction = "wide", 
        idvar=c("PatientNumber", "VisitDate"), 
        timevar="time")
#    PatientNumber VisitDate ICD9.1 ICD9.2
# 1             11  11/03/07     10     15
# 3             11  11/20/07     10     NA
# 4             22  12/20/08     30     NA
# 5             22  12/30/09     30     NA
# 6             33  09/20/12     25     60
# 8             33  10/25/07     25     NA
# 9             44  05/09/08     14     NA
# 10            55  06/09/13     40     13

, "", dcast "reshape2".

library(reshape2)
dcast(foo, PatientNumber + VisitDate ~ time, value.var="ICD9")
+6

,

library(dplyr)
library(tidyr) # See below on how to get tidyr

foo %>% 
  group_by(PatientNumber, VisitDate) %>%
  mutate(n=paste("ICD9",row_number(), sep="_")) %>%
  spread(n, ICD9)

 #Source: local data frame [8 x 4]

#  PatientNumber VisitDate ICD9_1 ICD9_2
#1            11  11/03/07     10     15
#2            11  11/20/07     10     NA
#3            22  12/20/08     30     NA
#4            22  12/30/09     30     NA
#5            33  09/20/12     25     60
#6            33  10/25/07     25     NA
#7            44  05/09/08     14     NA
#8            55  06/09/13     40     13

tidyr CRAN. (. tidyr git):

# install.packages("devtools")
devtools::install_github("hadley/tidyr")
+7

aggregate:

max_visits = 2
aggregate(ICD9 ~ PatientNumber + VisitDate, foo, 
          function(x) x[seq_len(max_visits)])  #note that output is 3 columns
#  PatientNumber VisitDate ICD9.1 ICD9.2
#1            44  05/09/08     14     NA
#2            55  06/09/13     40     13
#3            33  09/20/12     25     60
#4            33  10/25/07     25     NA
#5            11  11/03/07     10     15
#6            11  11/20/07     10     NA
#7            22  12/20/08     30     NA
#8            22  12/30/09     30     NA

( "max_visits" ), :

max_visits = max(ave(foo[["ICD9"]], 
                     foo[["PatientNumber"]], foo[["VisitDate"]],
                     FUN = length))
max_visits
#[1] 2

:

As @AnandaMahto noted in the comments, you can turn your 3-column aggregated "foo" (say "aggfoo") into 4 columns with something like:

dim(aggfoo)
#[1] 8 3
dim(do.call(data.frame, aggfoo))
#[1] 8 4
dim(data.frame(unclass(aggfoo)))
#[1] 8 4

This is not necessary, although, as with the three columns, it’s still convenient to name each column “ICD9”: aggfoo$ICD9[, 1]and aggfoo$ICD9[, 2]instead of aggfoo$ICD9.1and aggfoo$ICD9.2.

+2
source

Source: https://habr.com/ru/post/1546413/


All Articles