r - 使用 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"
解决方案
您拥有的是矩阵列表,这些数字是列名。您可以通过将它们设为空白来删除它们。
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"
#...
#...
推荐阅读
- pandas - 熊猫处理具有多个值的列,用分隔符分隔以进行数据分析
- function - IBM Watson Assistant 调用 IBM Cloud Function - 运行时错误
- php - 如何解决此问题网站在价格和描述选项卡下显示此参数错误?
- intellij-idea - 更新到新的 IntelliJ IDEA 版本后,sbt 任务配置丢失
- raku - 具有可变数量 Real 参数的脚本
- javascript - 如何确保 material-ui 中的整个 Button 组件为 onClick 事件返回相同的 ID 值?
- c++ - 编译时检查 C++ 中结构的更改
- javascript - clean-webpack-plugin 和 file-loader 的 CSS URL 问题
- android - 如何远程在 react-native-push-notifications 中设置自定义声音
- email - 当没有任何工作时,如何从 mac 终端发送邮件?