1

I've been on Stack Overflow for the past few days trying to get a solution to this issue I'm having.

I'm analyzing data received from the National Student Clearinghouse, specifically the graduations. So I have some dummy data

df <- data.frame(id=c('1', '1', '1'),   grad_date=c('20160501', '20170524', '20180524'),    order=c('1', '2', '3'),     inst_name=c('community college 1', 'univ 1', 'univ 2'),     inst_state=c('CA', 'CA', 'CA'),     level=c('Associate of Applied Sciences', 'Bachelors of Applied Sciences', 'Masters of Applied Sciences'),   deg_maj_1=c('NETWORK SECURITY', 'INFO ASSUR CYBR-SECURITY', 'CISCO CCNA PREPARATION'),  deg_cip_1=c('111003', '520299', '111003'),  deg_maj_2=c('NA', 'NA', 'NA'),  deg_cip_2=c('NA', 'NA', 'NA'),  deg_maj_3=c('NA', 'NA', 'NA'),  deg_cip_3=c('NA', 'NA', 'NA'),  deg_maj_4=c('NA', 'NA', 'NA'),  deg_cip_4=c('NA', 'NA', 'NA'))

and I'm trying to get this data wide:

df_wide<- dcast(df, id ~ order, value.var = c("inst_name", "inst_state", "level", "deg_maj_1", "deg_cip_1", "deg_maj_2", "deg_cip_2", "deg_maj_3", "deg_cip_3", "deg_maj_4", "deg_cip_4")) 

and I received this error:

Error in .subset2(x, i, exact = exact) : recursive indexing failed at level 2

I went here and here and got the same error

If this helps:

str(df)
'data.frame':   3 obs. of  14 variables:
 $ id        : Factor w/ 1 level "1": 1 1 1
 $ grad_date : Factor w/ 3 levels "20160501","20170524",..: 1 2 3
 $ order     : Factor w/ 3 levels "1","2","3": 1 2 3
 $ inst_name : Factor w/ 3 levels "community college 1",..: 1 2 3
 $ inst_state: Factor w/ 1 level "CA": 1 1 1
 $ level     : Factor w/ 3 levels "Associate of Applied Sciences",..: 1 2 3
 $ deg_maj_1 : Factor w/ 3 levels "CISCO CCNA PREPARATION",..: 3 2 1
 $ deg_cip_1 : Factor w/ 2 levels "111003","520299": 1 2 1
 $ deg_maj_2 : Factor w/ 1 level "NA": 1 1 1
 $ deg_cip_2 : Factor w/ 1 level "NA": 1 1 1
 $ deg_maj_3 : Factor w/ 1 level "NA": 1 1 1
 $ deg_cip_3 : Factor w/ 1 level "NA": 1 1 1
 $ deg_maj_4 : Factor w/ 1 level "NA": 1 1 1
 $ deg_cip_4 : Factor w/ 1 level "NA": 1 1 1

Can anyone assist? I'm at my wits end

Edited to add: Desired Output (yes I know it's looooooooooong but it is needed)

df_wide <- data.frame(id=c('1'), grad_date=c('20160501'), inst_name_1=c('community college 1'), inst_state_1=c('CA'), level_1=c('Associate of Applied Sciences'), deg_maj_1_1=c('NETWORK SECURITY'), deg_cip_1_1=c('111003'), deg_maj_2_1=c('NA'),  deg_cip_2_1=c('NA'), deg_maj_3_1=c('NA'), deg_cip_3_1=c('NA'), deg_maj_4_1=c('NA'), deg_cip_4_1=c('NA'), inst_name_2=c('univ 1'), inst_state_2=c('CA'), level_2=c('Bachelors of Applied Sciences'), deg_maj_1_2=c('INFO ASSUR CYBR-SECURITY'), deg_cip_1_2=c('520299'), deg_maj_2_2=c('NA'), deg_cip_2_2=c('NA'), deg_maj_3_2=c('NA'), deg_cip_3_2=c('NA'), deg_maj_4_2=c('NA'), deg_cip_4_2=c('NA'), inst_name_3=c('univ 2'), inst_state_3=c('CA'), level_3=c('Masters of Applied Sciences'), deg_maj_1_2=c('CISCO CCNA PREPARATION'), deg_cip_1_3=c('111003'), deg_maj_2_3=c('NA'), deg_cip_2_3=c('NA'), deg_maj_3_3=c('NA'), deg_cip_3_3=c('NA'), deg_maj_4_3=c('NA'), deg_cip_4_3=c('NA'))
tangerine7199
  • 443
  • 2
  • 8
  • 24

2 Answers2

1

If you don't rely on dcast(), base R's reshape() can get you there where you want.

reshape(df, idvar="id", timevar = "order", direction="wide")

Yielding

  id grad_date.1         inst_name.1 inst_state.1                       level.1
1  1    20160501 community college 1           CA Associate of Applied Sciences
       deg_maj_1.1 deg_cip_1.1 deg_maj_2.1 deg_cip_2.1 deg_maj_3.1 deg_cip_3.1
1 NETWORK SECURITY      111003          NA          NA          NA          NA
  deg_maj_4.1 deg_cip_4.1 grad_date.2 inst_name.2 inst_state.2
1          NA          NA    20170524      univ 1           CA
                        level.2              deg_maj_1.2 deg_cip_1.2 deg_maj_2.2
1 Bachelors of Applied Sciences INFO ASSUR CYBR-SECURITY      520299          NA
  deg_cip_2.2 deg_maj_3.2 deg_cip_3.2 deg_maj_4.2 deg_cip_4.2 grad_date.3 inst_name.3
1          NA          NA          NA          NA          NA    20180524      univ 2
  inst_state.3                     level.3            deg_maj_1.3 deg_cip_1.3
1           CA Masters of Applied Sciences CISCO CCNA PREPARATION      111003
  deg_maj_2.3 deg_cip_2.3 deg_maj_3.3 deg_cip_3.3 deg_maj_4.3 deg_cip_4.3
1          NA          NA          NA          NA          NA          NA
jay.sf
  • 60,139
  • 8
  • 53
  • 110
0

For the sake of completeness, the data.table version of dcast() is able to reshape multiple value columns simultaneously:

library(data.table)
dcast(setDT(df), id ~ order, value.var = tail(names(df), -3L))
   id         inst_name_1 inst_name_2 inst_name_3 inst_state_1 inst_state_2 inst_state_3                       level_1
1:  1 community college 1      univ 1      univ 2           CA           CA           CA Associate of Applied Sciences
                         level_2                     level_3      deg_maj_1_1              deg_maj_1_2
1: Bachelors of Applied Sciences Masters of Applied Sciences NETWORK SECURITY INFO ASSUR CYBR-SECURITY
              deg_maj_1_3 deg_cip_1_1 deg_cip_1_2 deg_cip_1_3 deg_maj_2_1 deg_maj_2_2 deg_maj_2_3 deg_cip_2_1 deg_cip_2_2
1: CISCO CCNA PREPARATION      111003      520299      111003          NA          NA          NA          NA          NA
   deg_cip_2_3 deg_maj_3_1 deg_maj_3_2 deg_maj_3_3 deg_cip_3_1 deg_cip_3_2 deg_cip_3_3 deg_maj_4_1 deg_maj_4_2 deg_maj_4_3
1:          NA          NA          NA          NA          NA          NA          NA          NA          NA          NA
   deg_cip_4_1 deg_cip_4_2 deg_cip_4_3
1:          NA          NA          NA
Uwe
  • 41,420
  • 11
  • 90
  • 134