如何为嵌套列表中的内部级别设置行名称?

huangapple go评论97阅读模式
英文:

How to set row names for inner levels in a nested list?

问题

在以下嵌套列表中,我需要将行名称设置为resource,行名称应从bodyPart中获取。我希望将此应用于所有对象(例如,obj1,obj2)。

  1. obj1 <- list(resource = list(bodyPart = c("leg", "arm", "knee"), side = c("LEFT", "RIGHT", "LEFT"), device = c("LLI", "LSM", "GHT")), cat = list(lab = c("aa", "bb", "cc")))
  2. obj2 <- list(resource = list(bodyPart = c("leg", "arm", "knee"), side = c("LEFT", "LEFT", "LEFT"), device = c("GOM", "LSM", "YYY")))
  3. x <- list(foo = c(fer = "wdb", obj1), bar = obj2)

我希望输出看起来像这样,例如对于obj2。

bodyPart side device
leg leg LEFT GOM
arm arm LEFT LSM
knee knee LEFT YYY

感谢您的建议。

英文:

In the nested list below, I need to set row names to resource and the row names should be taken from bodyPart. I want this to be applied to all objects (e.g., obj1, obj2)

  1. obj1 &lt;- list(resource = list(bodyPart = c(&quot;leg&quot;, &quot;arm&quot;, &quot;knee&quot;),side = c(&quot;LEFT&quot;, &quot;RIGHT&quot;, &quot;LEFT&quot;), device = c(&quot;LLI&quot;, &quot;LSM&quot;, &quot;GHT&quot;)), cat = list(lab = c(&quot;aa&quot;, &quot;bb&quot;, &quot;cc&quot;)))
  2. obj2 &lt;- list(resource = list(bodyPart = c(&quot;leg&quot;, &quot;arm&quot;, &quot;knee&quot;), side = c(&quot;LEFT&quot;, &quot;LEFT&quot;, &quot;LEFT&quot;), device = c(&quot;GOM&quot;, &quot;LSM&quot;, &quot;YYY&quot;)))
  3. x &lt;- list(foo = c(fer = &quot;wdb&quot;, obj1), bar = obj2)

I would like the output to look like this at resource level, for instance for obj2.

bodyPart side device
leg leg LEFT GOM
arm arm LEFT LSM
knee knee LEFT YYY

I appreciate your edvice.

答案1

得分: 1

Alternatively we could use

  1. obj2$resource %>% as.data.frame() %>% mutate(bodyPart2=bodyPart) %>%
  2. column_to_rownames(var = 'bodyPart2')

output

  1. bodyPart side device

leg leg LEFT GOM
arm arm LEFT LSM
knee knee LEFT YYY

  1. <details>
  2. <summary>英文:</summary>
  3. Alternatively we could use
  4. ```r
  5. obj2$resource %&gt;% as.data.frame() %&gt;% mutate(bodyPart2=bodyPart) %&gt;%
  6. column_to_rownames(var = &#39;bodyPart2&#39;)
  7. # output
  8. bodyPart side device
  9. leg leg LEFT GOM
  10. arm arm LEFT LSM
  11. knee knee LEFT YYY

答案2

得分: 0

已编辑为双重lapply

  1. lapply(x, function(y) {
  2. lapply(y, function(z) {
  3. if ("bodyPart" %in% names(z)) {
  4. `rownames<-`(as.data.frame(z), z[["bodyPart"]])
  5. } else z
  6. })
  7. })
  8. # $foo
  9. # $foo$fer
  10. # [1] "wdb"
  11. # $foo$resource
  12. # bodyPart side device
  13. # leg leg LEFT LLI
  14. # arm arm RIGHT LSM
  15. # knee knee LEFT GHT
  16. # $foo$cat
  17. # $foo$cat$lab
  18. # [1] "aa" "bb" "cc"
  19. # $bar
  20. # $bar$resource
  21. # bodyPart side device
  22. # leg leg LEFT GOM
  23. # arm arm LEFT LSM
  24. # knee knee LEFT YYY
英文:

Edited to make it a double-lapply:

  1. lapply(x, function(y) {
  2. lapply(y, function(z) {
  3. if (&quot;bodyPart&quot; %in% names(z)) {
  4. `rownames&lt;-`(as.data.frame(z), z[[&quot;bodyPart&quot;]])
  5. } else z
  6. )
  7. )
  8. # $foo
  9. # $foo$fer
  10. # [1] &quot;wdb&quot;
  11. # $foo$resource
  12. # bodyPart side device
  13. # leg leg LEFT LLI
  14. # arm arm RIGHT LSM
  15. # knee knee LEFT GHT
  16. # $foo$cat
  17. # $foo$cat$lab
  18. # [1] &quot;aa&quot; &quot;bb&quot; &quot;cc&quot;
  19. # $bar
  20. # $bar$resource
  21. # bodyPart side device
  22. # leg leg LEFT GOM
  23. # arm arm LEFT LSM
  24. # knee knee LEFT YYY

huangapple
  • 本文由 发表于 2023年6月29日 23:39:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定