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

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

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

问题

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

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")))
obj2 <- list(resource = list(bodyPart = c("leg", "arm", "knee"), side = c("LEFT", "LEFT", "LEFT"), device = c("GOM", "LSM", "YYY")))

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)

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

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

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

output

 bodyPart side device

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


<details>
<summary>英文:</summary>

Alternatively we could use 

```r
obj2$resource %&gt;% as.data.frame() %&gt;% mutate(bodyPart2=bodyPart) %&gt;% 
column_to_rownames(var = &#39;bodyPart2&#39;)

# output

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

答案2

得分: 0

已编辑为双重lapply

lapply(x, function(y) {
  lapply(y, function(z) {
    if ("bodyPart" %in% names(z)) {
      `rownames<-`(as.data.frame(z), z[["bodyPart"]])
    } else z
  })
})
# $foo
# $foo$fer
# [1] "wdb"
# $foo$resource
#      bodyPart  side device
# leg       leg  LEFT    LLI
# arm       arm RIGHT    LSM
# knee     knee  LEFT    GHT
# $foo$cat
# $foo$cat$lab
# [1] "aa" "bb" "cc"
# $bar
# $bar$resource
#      bodyPart side device
# leg       leg LEFT    GOM
# arm       arm LEFT    LSM
# knee     knee LEFT    YYY
英文:

Edited to make it a double-lapply:

lapply(x, function(y) {
  lapply(y, function(z) {
    if (&quot;bodyPart&quot; %in% names(z)) {
      `rownames&lt;-`(as.data.frame(z), z[[&quot;bodyPart&quot;]])
    } else z
  )
)
# $foo
# $foo$fer
# [1] &quot;wdb&quot;
# $foo$resource
#      bodyPart  side device
# leg       leg  LEFT    LLI
# arm       arm RIGHT    LSM
# knee     knee  LEFT    GHT
# $foo$cat
# $foo$cat$lab
# [1] &quot;aa&quot; &quot;bb&quot; &quot;cc&quot;
# $bar
# $bar$resource
#      bodyPart side device
# leg       leg LEFT    GOM
# arm       arm LEFT    LSM
# 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:

确定