首页 > 解决方案 > 使用 lapply(t) 转置列表时忽略行号

问题描述

我有一些代码可以将我的数据帧列表从水平格式转换为垂直格式。当我检查列表时,我注意到数据帧的行号被包括在内(664、665、670 等)。为了转置列表,我使用 lapply(t) ,它除了包含那些讨厌的行号外,现在使结果列表条目不是数据框。我是使用 lapply 的新手;有没有办法使用 lapply 来转置我的列表,同时排除这些数字?

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" 

编辑:

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")))

我想要的是要排除的数字(即 664、665 和 670)。这样它看起来像这样:

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" 

标签: rlapplytranspose

解决方案


您拥有的是矩阵列表,这些数字是列名。您可以通过将它们设为空白来删除它们。

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" 

#...
#...            

推荐阅读