Search code examples
rlapplytranspose

Ignore row numbers when transposing a list using lapply(t)


I have some code that tranposes my list of dataframes from horizontal format to vertical format. When I inspect the list, I notice that the row numbers of the dataframe are included (664, 665, 670, etc.). To transpose the list, I use lapply(t) which works except that those pesky row numbers are included and now make the resulting list entries not a data frame. I am new to using lapply; Is there a way to use lapply to transpose my list while also excluding those numbers?

print(cda)
$`1`
                            664                                
category                    "Changes in Daily Activity"        
Variable Name               "NOTICE"                           
Variabe Description / Label "When did you first notice decline"
Variable Format             "DATE9."                           
Variable Allowable Values   ""                                 
Method of Data Collection   "Informant"                        
Data Type                   "Num"                              

$`2`
                            665                        
category                    "Changes in Daily Activity"
Variable Name               "MONTHS"                   
Variabe Description / Label "Number of months elapsed" 
Variable Format             "3."                       
Variable Allowable Values   ""                         
Method of Data Collection   "Informant"                
Data Type                   "Num"                      

....                                 

$`6`
                            670                                                                                                   
category                    "Changes in Daily Activity"                                                                           
Variable Name               "COURMEM"                                                                                             
Variabe Description / Label "Has the course of the decline been a steady downhill progression or have there been abrupt declines?"
Variable Format             "1."                                                                                                  
Variable Allowable Values   "1 = Steady\n2 = Abrupt\n3 = Not Known"                                                               
Method of Data Collection   "Informant"                                                                                           
Data Type                   "Num" 

EDIT:

list(`1` = structure(c("Changes in Daily Activity", "NOTICE", 
"When did you first notice decline", "DATE9.", "", "Informant", 
"Num"), .Dim = c(7L, 1L), .Dimnames = list(c("category", "Variable Name", 
"Variabe Description / Label", "Variable Format", "Variable Allowable Values", 
"Method of Data Collection", "Data Type"), "664")), `2` = structure(c("Changes in Daily Activity", 
"MONTHS", "Number of months elapsed", "3.", "", "Informant", 
"Num"), .Dim = c(7L, 1L), .Dimnames = list(c("category", "Variable Name", 
"Variabe Description / Label", "Variable Format", "Variable Allowable Values", 
"Method of Data Collection", "Data Type"), "665")), `3` = structure(c("Changes in Daily Activity", 
"SLOWDECL", "Did this happen slowly or suddenly?", "1", "1 = Slowly\n2 = Suddenly\n3 = Not Known", 
"Informant", "Num"), .Dim = c(7L, 1L), .Dimnames = list(c("category", 
"Variable Name", "Variabe Description / Label", "Variable Format", 
"Variable Allowable Values", "Method of Data Collection", "Data Type"
), "666")), `4` = structure(c("Changes in Daily Activity", "COURMENT", 
"Has the course of the decline been a steady downhill progression or have there been abrupt declines?", 
"1", "1 = Steady\n2 = Abrupt\n3 = Not Known", "Informant", "Num"
), .Dim = c(7L, 1L), .Dimnames = list(c("category", "Variable Name", 
"Variabe Description / Label", "Variable Format", "Variable Allowable Values", 
"Method of Data Collection", "Data Type"), "667")), `5` = structure(c("Changes in Daily Activity", 
"SLOWREM", "Did this happen slowly or suddenly?", "1.", "1 = Slowly\n2 = Suddenly\n3 = Not Known", 
"Informant", "Num"), .Dim = c(7L, 1L), .Dimnames = list(c("category", 
"Variable Name", "Variabe Description / Label", "Variable Format", 
"Variable Allowable Values", "Method of Data Collection", "Data Type"
), "669")), `6` = structure(c("Changes in Daily Activity", "COURMEM", 
"Has the course of the decline been a steady downhill progression or have there been abrupt declines?", 
"1.", "1 = Steady\n2 = Abrupt\n3 = Not Known", "Informant", "Num"
), .Dim = c(7L, 1L), .Dimnames = list(c("category", "Variable Name", 
"Variabe Description / Label", "Variable Format", "Variable Allowable Values", 
"Method of Data Collection", "Data Type"), "670")))

What I would like is the numbers to be excluded (i.e. 664, 665, and 670). That way it will look like this:

print(cda)
$`1`
                                                           
category                    "Changes in Daily Activity"        
Variable Name               "NOTICE"                           
Variabe Description / Label "When did you first notice decline"
Variable Format             "DATE9."                           
Variable Allowable Values   ""                                 
Method of Data Collection   "Informant"                        
Data Type                   "Num"                              

$`2`
                                                   
category                    "Changes in Daily Activity"
Variable Name               "MONTHS"                   
Variabe Description / Label "Number of months elapsed" 
Variable Format             "3."                       
Variable Allowable Values   ""                         
Method of Data Collection   "Informant"                
Data Type                   "Num"                      

....                                 

$`6`
                                                                                                                               
category                    "Changes in Daily Activity"                                                                           
Variable Name               "COURMEM"                                                                                             
Variabe Description / Label "Has the course of the decline been a steady downhill progression or have there been abrupt declines?"
Variable Format             "1."                                                                                                  
Variable Allowable Values   "1 = Steady\n2 = Abrupt\n3 = Not Known"                                                               
Method of Data Collection   "Informant"                                                                                           
Data Type                   "Num" 

Solution

  • What you have is list of matrices and those numbers are column names. You can remove them by making them blank.

    cda <- lapply(cda, function(x) {colnames(x) <- '';x})
    cda
    
    #$`1`
                                                                   
    #category                    "Changes in Daily Activity"        
    #Variable Name               "NOTICE"                           
    #Variabe Description / Label "When did you first notice decline"
    #Variable Format             "DATE9."                           
    #Variable Allowable Values   ""                                 
    #Method of Data Collection   "Informant"                        
    #Data Type                   "Num"                              
    
    #$`2`
                                                           
    #category                    "Changes in Daily Activity"
    #Variable Name               "MONTHS"                   
    #Variabe Description / Label "Number of months elapsed" 
    #Variable Format             "3."                       
    #Variable Allowable Values   ""                         
    #Method of Data Collection   "Informant"                
    #Data Type                   "Num" 
    
    #...
    #...